home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / readline / readline.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-24  |  151.0 KB  |  6,534 lines

  1. /* readline.c -- a general facility for reading lines of input
  2.    with emacs style editing and completion. */
  3.  
  4. /* Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.
  5.  
  6.    This file contains the Readline Library (the Library), a set of
  7.    routines for providing Emacs style line input to programs that ask
  8.    for it.
  9.  
  10.    The Library is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 2 of the License, or
  13.    (at your option) any later version.
  14.  
  15.    The Library is distributed in the hope that it will be useful,
  16.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.    GNU General Public License for more details.
  19.  
  20.    You should have received a copy of the GNU General Public License
  21.    along with this program; if not, write to the Free Software
  22.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  23.  
  24. /* Remove these declarations when we have a complete libgnu.a. */
  25. /* #define STATIC_MALLOC */
  26. #if !defined (STATIC_MALLOC)
  27. extern char *xmalloc (), *xrealloc ();
  28. #else
  29. static char *xmalloc (), *xrealloc ();
  30. #endif /* STATIC_MALLOC */
  31.  
  32. #include "sysdep.h"
  33. #include <sys/types.h>
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #ifndef    NO_SYS_FILE
  37. #include <sys/file.h>
  38. #endif
  39. #include <signal.h>
  40. #include <sys/dir.h>
  41.  
  42. #if defined (HAVE_UNISTD_H)
  43. #  include <unistd.h>
  44. #endif
  45.  
  46. #define NEW_TTY_DRIVER
  47. #define HAVE_BSD_SIGNALS
  48. /* #define USE_XON_XOFF */
  49.  
  50. #ifdef __MSDOS__
  51. #undef NEW_TTY_DRIVER
  52. #undef HAVE_BSD_SIGNALS
  53. #endif
  54.  
  55. /* Some USG machines have BSD signal handling (sigblock, sigsetmask, etc.) */
  56. #if defined (USG) && !defined (hpux)
  57. #undef HAVE_BSD_SIGNALS
  58. #endif
  59.  
  60. /* System V machines use termio. */
  61. #if !defined (_POSIX_VERSION)
  62. #  if defined (USG) || defined (hpux) || defined (Xenix) || defined (sgi) || defined (DGUX)
  63. #    undef NEW_TTY_DRIVER
  64. #    define TERMIO_TTY_DRIVER
  65. #    include <termio.h>
  66. #    if !defined (TCOON)
  67. #      define TCOON 1
  68. #    endif
  69. #  endif /* USG || hpux || Xenix || sgi || DUGX */
  70. #endif /* !_POSIX_VERSION */
  71.  
  72. /* Posix systems use termios and the Posix signal functions. */
  73. #if defined (_POSIX_VERSION)
  74. #  if !defined (TERMIOS_MISSING)
  75. #    undef NEW_TTY_DRIVER
  76. #    define TERMIOS_TTY_DRIVER
  77. #    include <termios.h>
  78. #  endif /* !TERMIOS_MISSING */
  79. #  define HAVE_POSIX_SIGNALS
  80. #  if !defined (O_NDELAY)
  81. #    define O_NDELAY O_NONBLOCK    /* Posix-style non-blocking i/o */
  82. #  endif /* O_NDELAY */
  83. #endif /* _POSIX_VERSION */
  84.  
  85. /* Other (BSD) machines use sgtty. */
  86. #if defined (NEW_TTY_DRIVER)
  87. #include <sgtty.h>
  88. #endif
  89.  
  90. /* Define _POSIX_VDISABLE if we are not using the `new' tty driver and
  91.    it is not already defined.  It is used both to determine if a
  92.    special character is disabled and to disable certain special
  93.    characters.  Posix systems should set to 0, USG systems to -1. */
  94. #if !defined (NEW_TTY_DRIVER) && !defined (_POSIX_VDISABLE)
  95. #  if defined (_POSIX_VERSION)
  96. #    define _POSIX_VDISABLE 0
  97. #  else /* !_POSIX_VERSION */
  98. #    define _POSIX_VDISABLE -1
  99. #  endif /* !_POSIX_VERSION */
  100. #endif /* !NEW_TTY_DRIVER && !_POSIX_VDISABLE */
  101.  
  102. #include <errno.h>
  103. extern int errno;
  104.  
  105. #include <setjmp.h>
  106. #include <sys/stat.h>
  107.  
  108. /* Posix macro to check file in statbuf for directory-ness. */
  109. #if defined (S_IFDIR) && !defined (S_ISDIR)
  110. #define S_ISDIR(m) (((m)&S_IFMT) == S_IFDIR)
  111. #endif
  112.  
  113. #ifndef __MSDOS__
  114. /* These next are for filename completion.  Perhaps this belongs
  115.    in a different place. */
  116. #include <pwd.h>
  117. #endif /* __MSDOS__ */
  118.  
  119. #if defined (USG) && !defined (isc386) && !defined (sgi)
  120. struct passwd *getpwuid (), *getpwent ();
  121. #endif
  122.  
  123. /* #define HACK_TERMCAP_MOTION */
  124.  
  125. /* Some standard library routines. */
  126. #include "readline.h"
  127. #include "history.h"
  128.  
  129. #ifndef digit
  130. #define digit(c)  ((c) >= '0' && (c) <= '9')
  131. #endif
  132.  
  133. #ifndef isletter
  134. #define isletter(c) (((c) >= 'A' && (c) <= 'Z') || ((c) >= 'a' && (c) <= 'z'))
  135. #endif
  136.  
  137. #ifndef digit_value
  138. #define digit_value(c) ((c) - '0')
  139. #endif
  140.  
  141. #ifndef member
  142. #define member(c, s) ((c) ? index ((s), (c)) : 0)
  143. #endif
  144.  
  145. #ifndef isident
  146. #define isident(c) ((isletter(c) || digit(c) || c == '_'))
  147. #endif
  148.  
  149. #ifndef exchange
  150. #define exchange(x, y) {int temp = x; x = y; y = temp;}
  151. #endif
  152.  
  153. #if !defined (rindex)
  154. extern char *rindex ();
  155. #endif /* rindex */
  156.  
  157. #if !defined (index)
  158. extern char *index ();
  159. #endif /* index */
  160.  
  161. extern char *getenv ();
  162. extern char *tilde_expand ();
  163.  
  164. static update_line ();
  165. static void output_character_function ();
  166. static delete_chars ();
  167. static insert_some_chars ();
  168.  
  169. #if defined (VOID_SIGHANDLER)
  170. #  define sighandler void
  171. #else
  172. #  define sighandler int
  173. #endif /* VOID_SIGHANDLER */
  174.  
  175. /* This typedef is equivalant to the one for Function; it allows us
  176.    to say SigHandler *foo = signal (SIGKILL, SIG_IGN); */
  177. typedef sighandler SigHandler ();
  178.  
  179. /* If on, then readline handles signals in a way that doesn't screw. */
  180. #define HANDLE_SIGNALS
  181.  
  182. #ifdef __GO32__
  183. #include <pc.h>
  184. #undef HANDLE_SIGNALS
  185. #endif
  186.  
  187.  
  188. /* **************************************************************** */
  189. /*                                    */
  190. /*            Line editing input utility            */
  191. /*                                    */
  192. /* **************************************************************** */
  193.  
  194. /* A pointer to the keymap that is currently in use.
  195.    By default, it is the standard emacs keymap. */
  196. Keymap keymap = emacs_standard_keymap;
  197.  
  198. #define no_mode -1
  199. #define vi_mode 0
  200. #define emacs_mode 1
  201.  
  202. /* The current style of editing. */
  203. int rl_editing_mode = emacs_mode;
  204.  
  205. /* Non-zero if the previous command was a kill command. */
  206. static int last_command_was_kill = 0;
  207.  
  208. /* The current value of the numeric argument specified by the user. */
  209. int rl_numeric_arg = 1;
  210.  
  211. /* Non-zero if an argument was typed. */
  212. int rl_explicit_arg = 0;
  213.  
  214. /* Temporary value used while generating the argument. */
  215. int rl_arg_sign = 1;
  216.  
  217. /* Non-zero means we have been called at least once before. */
  218. static int rl_initialized = 0;
  219.  
  220. /* If non-zero, this program is running in an EMACS buffer. */
  221. static char *running_in_emacs = (char *)NULL;
  222.  
  223. /* The current offset in the current input line. */
  224. int rl_point;
  225.  
  226. /* Mark in the current input line. */
  227. int rl_mark;
  228.  
  229. /* Length of the current input line. */
  230. int rl_end;
  231.  
  232. /* Make this non-zero to return the current input_line. */
  233. int rl_done;
  234.  
  235. /* The last function executed by readline. */
  236. Function *rl_last_func = (Function *)NULL;
  237.  
  238. /* Top level environment for readline_internal (). */
  239. static jmp_buf readline_top_level;
  240.  
  241. /* The streams we interact with. */
  242. static FILE *in_stream, *out_stream;
  243.  
  244. /* The names of the streams that we do input and output to. */
  245. FILE *rl_instream = stdin, *rl_outstream = stdout;
  246.  
  247. /* Non-zero means echo characters as they are read. */
  248. int readline_echoing_p = 1;
  249.  
  250. /* Current prompt. */
  251. char *rl_prompt;
  252.  
  253. /* The number of characters read in order to type this complete command. */
  254. int rl_key_sequence_length = 0;
  255.  
  256. /* If non-zero, then this is the address of a function to call just
  257.    before readline_internal () prints the first prompt. */
  258. Function *rl_startup_hook = (Function *)NULL;
  259.  
  260. /* If non-zero, then this is the address of a function to call when
  261.    completing on a directory name.  The function is called with
  262.    the address of a string (the current directory name) as an arg. */
  263. Function *rl_symbolic_link_hook = (Function *)NULL;
  264.  
  265. /* What we use internally.  You should always refer to RL_LINE_BUFFER. */
  266. static char *the_line;
  267.  
  268. /* The character that can generate an EOF.  Really read from
  269.    the terminal driver... just defaulted here. */
  270. static int eof_char = CTRL ('D');
  271.  
  272. /* Non-zero makes this the next keystroke to read. */
  273. int rl_pending_input = 0;
  274.  
  275. /* Pointer to a useful terminal name. */
  276. char *rl_terminal_name = (char *)NULL;
  277.  
  278. /* Line buffer and maintenence. */
  279. char *rl_line_buffer = (char *)NULL;
  280. int rl_line_buffer_len = 0;
  281. #define DEFAULT_BUFFER_SIZE 256
  282.  
  283.  
  284. /* **************************************************************** */
  285. /*                                    */
  286. /*            `Forward' declarations              */
  287. /*                                    */
  288. /* **************************************************************** */
  289.  
  290. /* Non-zero means do not parse any lines other than comments and
  291.    parser directives. */
  292. static unsigned char parsing_conditionalized_out = 0;
  293.  
  294. /* Caseless strcmp (). */
  295. static int stricmp (), strnicmp ();
  296.  
  297. /* Non-zero means to save keys that we dispatch on in a kbd macro. */
  298. static int defining_kbd_macro = 0;
  299.  
  300.  
  301. /* **************************************************************** */
  302. /*                                    */
  303. /*            Top Level Functions                */
  304. /*                                    */
  305. /* **************************************************************** */
  306.  
  307. static void rl_prep_terminal (), rl_deprep_terminal ();
  308.  
  309. /* Read a line of input.  Prompt with PROMPT.  A NULL PROMPT means
  310.    none.  A return value of NULL means that EOF was encountered. */
  311. char *
  312. readline (prompt)
  313.      char *prompt;
  314. {
  315.   char *readline_internal ();
  316.   char *value;
  317.  
  318.   rl_prompt = prompt;
  319.  
  320.   /* If we are at EOF return a NULL string. */
  321.   if (rl_pending_input == EOF)
  322.     {
  323.       rl_pending_input = 0;
  324.       return ((char *)NULL);
  325.     }
  326.  
  327.   rl_initialize ();
  328.   rl_prep_terminal ();
  329.  
  330. #if defined (HANDLE_SIGNALS)
  331.   rl_set_signals ();
  332. #endif
  333.  
  334.   value = readline_internal ();
  335.   rl_deprep_terminal ();
  336.  
  337. #if defined (HANDLE_SIGNALS)
  338.   rl_clear_signals ();
  339. #endif
  340.  
  341.   return (value);
  342. }
  343.  
  344. /* Read a line of input from the global rl_instream, doing output on
  345.    the global rl_outstream.
  346.    If rl_prompt is non-null, then that is our prompt. */
  347. char *
  348. readline_internal ()
  349. {
  350.   int lastc, c, eof_found;
  351.  
  352.   in_stream  = rl_instream;
  353.   out_stream = rl_outstream;
  354.  
  355.   lastc = -1;
  356.   eof_found = 0;
  357.  
  358.   if (rl_startup_hook)
  359.     (*rl_startup_hook) ();
  360.  
  361.   if (!readline_echoing_p)
  362.     {
  363.       if (rl_prompt)
  364.     {
  365.       fprintf (out_stream, "%s", rl_prompt);
  366.       fflush (out_stream);
  367.     }
  368.     }
  369.   else
  370.     {
  371.       rl_on_new_line ();
  372.       rl_redisplay ();
  373. #if defined (VI_MODE)
  374.       if (rl_editing_mode == vi_mode)
  375.     rl_vi_insertion_mode ();
  376. #endif /* VI_MODE */
  377.     }
  378.  
  379.   while (!rl_done)
  380.     {
  381.       int lk = last_command_was_kill;
  382.       int code = setjmp (readline_top_level);
  383.  
  384.       if (code)
  385.     rl_redisplay ();
  386.  
  387.       if (!rl_pending_input)
  388.     {
  389.       /* Then initialize the argument and number of keys read. */
  390.       rl_init_argument ();
  391.       rl_key_sequence_length = 0;
  392.     }
  393.  
  394.       c = rl_read_key ();
  395.  
  396.       /* EOF typed to a non-blank line is a <NL>. */
  397.       if (c == EOF && rl_end)
  398.     c = NEWLINE;
  399.  
  400.       /* The character eof_char typed to blank line, and not as the
  401.      previous character is interpreted as EOF. */
  402.       if (((c == eof_char && lastc != c) || c == EOF) && !rl_end)
  403.     {
  404.       eof_found = 1;
  405.       break;
  406.     }
  407.  
  408.       lastc = c;
  409.       rl_dispatch (c, keymap);
  410.  
  411.       /* If there was no change in last_command_was_kill, then no kill
  412.      has taken place.  Note that if input is pending we are reading
  413.      a prefix command, so nothing has changed yet. */
  414.       if (!rl_pending_input)
  415.     {
  416.       if (lk == last_command_was_kill)
  417.         last_command_was_kill = 0;
  418.     }
  419.  
  420. #if defined (VI_MODE)
  421.       /* In vi mode, when you exit insert mode, the cursor moves back
  422.      over the previous character.  We explicitly check for that here. */
  423.       if (rl_editing_mode == vi_mode && keymap == vi_movement_keymap)
  424.     rl_vi_check ();
  425. #endif /* VI_MODE */
  426.  
  427.       if (!rl_done)
  428.     rl_redisplay ();
  429.     }
  430.  
  431.   /* Restore the original of this history line, iff the line that we
  432.      are editing was originally in the history, AND the line has changed. */
  433.   {
  434.     HIST_ENTRY *entry = current_history ();
  435.  
  436.     if (entry && rl_undo_list)
  437.       {
  438.     char *temp = savestring (the_line);
  439.     rl_revert_line ();
  440.     entry = replace_history_entry (where_history (), the_line,
  441.                        (HIST_ENTRY *)NULL);
  442.     free_history_entry (entry);
  443.  
  444.     strcpy (the_line, temp);
  445.     free (temp);
  446.       }
  447.   }
  448.  
  449.   /* At any rate, it is highly likely that this line has an undo list.  Get
  450.      rid of it now. */
  451.   if (rl_undo_list)
  452.     free_undo_list ();
  453.  
  454.   if (eof_found)
  455.     return (char *)NULL;
  456.   else
  457.     return (savestring (the_line));
  458. }
  459.  
  460.  
  461. /* **************************************************************** */
  462. /*                                        */
  463. /*               Signal Handling                          */
  464. /*                                    */
  465. /* **************************************************************** */
  466.  
  467. #if defined (SIGWINCH)
  468. static SigHandler *old_sigwinch = (SigHandler *)NULL;
  469.  
  470. static sighandler
  471. rl_handle_sigwinch (sig)
  472.      int sig;
  473. {
  474.   char *term;
  475.  
  476.   term = rl_terminal_name;
  477.  
  478.   if (readline_echoing_p)
  479.     {
  480.       if (!term)
  481.     term = getenv ("TERM");
  482.       if (!term)
  483.     term = "dumb";
  484.       rl_reset_terminal (term);
  485. #if defined (NOTDEF)
  486.       crlf ();
  487.       rl_forced_update_display ();
  488. #endif /* NOTDEF */
  489.     }
  490.  
  491.   if (old_sigwinch &&
  492.       old_sigwinch != (SigHandler *)SIG_IGN &&
  493.       old_sigwinch != (SigHandler *)SIG_DFL)
  494.     (*old_sigwinch) (sig);
  495. #if !defined (VOID_SIGHANDLER)
  496.   return (0);
  497. #endif /* VOID_SIGHANDLER */
  498. }
  499. #endif  /* SIGWINCH */
  500.  
  501. #if defined (HANDLE_SIGNALS)
  502. /* Interrupt handling. */
  503. static SigHandler
  504.   *old_int  = (SigHandler *)NULL,
  505.   *old_tstp = (SigHandler *)NULL,
  506.   *old_ttou = (SigHandler *)NULL,
  507.   *old_ttin = (SigHandler *)NULL,
  508.   *old_cont = (SigHandler *)NULL,
  509.   *old_alrm = (SigHandler *)NULL;
  510.  
  511. /* Handle an interrupt character. */
  512. static sighandler
  513. rl_signal_handler (sig)
  514.      int sig;
  515. {
  516. #if !defined (HAVE_BSD_SIGNALS)
  517.   /* Since the signal will not be blocked while we are in the signal
  518.      handler, ignore it until rl_clear_signals resets the catcher. */
  519.   if (sig == SIGINT)
  520.     signal (sig, SIG_IGN);
  521. #endif /* !HAVE_BSD_SIGNALS */
  522.  
  523.   switch (sig)
  524.     {
  525.     case SIGINT:
  526.       free_undo_list ();
  527.       rl_clear_message ();
  528.       rl_init_argument ();
  529.  
  530. #if defined (SIGTSTP)
  531.     case SIGTSTP:
  532.     case SIGTTOU:
  533.     case SIGTTIN:
  534. #endif /* SIGTSTP */
  535.     case SIGALRM:
  536.       rl_clean_up_for_exit ();
  537.       rl_deprep_terminal ();
  538.       rl_clear_signals ();
  539.       rl_pending_input = 0;
  540.  
  541.       kill (getpid (), sig);
  542.  
  543. #if defined (HAVE_POSIX_SIGNALS)
  544.       {
  545.     sigset_t set;
  546.  
  547.     sigemptyset (&set);
  548.     sigprocmask (SIG_SETMASK, &set, (sigset_t *)NULL);
  549.       }
  550. #else
  551. #if defined (HAVE_BSD_SIGNALS)
  552.       sigsetmask (0);
  553. #endif /* HAVE_BSD_SIGNALS */
  554. #endif /* HAVE_POSIX_SIGNALS */
  555.  
  556.       rl_prep_terminal ();
  557.       rl_set_signals ();
  558.     }
  559.  
  560. #if !defined (VOID_SIGHANDLER)
  561.   return (0);
  562. #endif /* !VOID_SIGHANDLER */
  563. }
  564.  
  565. rl_set_signals ()
  566. {
  567.   old_int = (SigHandler *)signal (SIGINT, rl_signal_handler);
  568.   if (old_int == (SigHandler *)SIG_IGN)
  569.     signal (SIGINT, SIG_IGN);
  570.  
  571.   old_alrm = (SigHandler *)signal (SIGALRM, rl_signal_handler);
  572.   if (old_alrm == (SigHandler *)SIG_IGN)
  573.     signal (SIGALRM, SIG_IGN);
  574.  
  575. #if defined (SIGTSTP)
  576.   old_tstp = (SigHandler *)signal (SIGTSTP, rl_signal_handler);
  577.   if (old_tstp == (SigHandler *)SIG_IGN)
  578.     signal (SIGTSTP, SIG_IGN);
  579. #endif
  580. #if defined (SIGTTOU)
  581.   old_ttou = (SigHandler *)signal (SIGTTOU, rl_signal_handler);
  582.   old_ttin = (SigHandler *)signal (SIGTTIN, rl_signal_handler);
  583.  
  584.   if (old_tstp == (SigHandler *)SIG_IGN)
  585.     {
  586.       signal (SIGTTOU, SIG_IGN);
  587.       signal (SIGTTIN, SIG_IGN);
  588.     }
  589. #endif
  590.  
  591. #if defined (SIGWINCH)
  592.   old_sigwinch = (SigHandler *)signal (SIGWINCH, rl_handle_sigwinch);
  593. #endif
  594. }
  595.  
  596. rl_clear_signals ()
  597. {
  598.   signal (SIGINT, old_int);
  599.   signal (SIGALRM, old_alrm);
  600.  
  601. #if defined (SIGTSTP)
  602.   signal (SIGTSTP, old_tstp);
  603. #endif
  604.  
  605. #if defined (SIGTTOU)
  606.   signal (SIGTTOU, old_ttou);
  607.   signal (SIGTTIN, old_ttin);
  608. #endif
  609.  
  610. #if defined (SIGWINCH)
  611.       signal (SIGWINCH, old_sigwinch);
  612. #endif
  613. }
  614. #endif  /* HANDLE_SIGNALS */
  615.  
  616.  
  617. /* **************************************************************** */
  618. /*                                    */
  619. /*            Character Input Buffering               */
  620. /*                                    */
  621. /* **************************************************************** */
  622.  
  623. #if defined (USE_XON_XOFF)
  624. /* If the terminal was in xoff state when we got to it, then xon_char
  625.    contains the character that is supposed to start it again. */
  626. static int xon_char, xoff_state;
  627. #endif /* USE_XON_XOFF */
  628.  
  629. static int pop_index = 0, push_index = 0, ibuffer_len = 511;
  630. static unsigned char ibuffer[512];
  631.  
  632. /* Non-null means it is a pointer to a function to run while waiting for
  633.    character input. */
  634. Function *rl_event_hook = (Function *)NULL;
  635.  
  636. #define any_typein (push_index != pop_index)
  637.  
  638. /* Add KEY to the buffer of characters to be read. */
  639. rl_stuff_char (key)
  640.      int key;
  641. {
  642.   if (key == EOF)
  643.     {
  644.       key = NEWLINE;
  645.       rl_pending_input = EOF;
  646.     }
  647.   ibuffer[push_index++] = key;
  648.   if (push_index >= ibuffer_len)
  649.     push_index = 0;
  650. }
  651.  
  652. /* Return the amount of space available in the
  653.    buffer for stuffing characters. */
  654. int
  655. ibuffer_space ()
  656. {
  657.   if (pop_index > push_index)
  658.     return (pop_index - push_index);
  659.   else
  660.     return (ibuffer_len - (push_index - pop_index));
  661. }
  662.  
  663. /* Get a key from the buffer of characters to be read.
  664.    Return the key in KEY.
  665.    Result is KEY if there was a key, or 0 if there wasn't. */
  666. int
  667. rl_get_char (key)
  668.      int *key;
  669. {
  670.   if (push_index == pop_index)
  671.     return (0);
  672.  
  673.   *key = ibuffer[pop_index++];
  674.  
  675.   if (pop_index >= ibuffer_len)
  676.     pop_index = 0;
  677.  
  678.   return (1);
  679. }
  680.  
  681. /* Stuff KEY into the *front* of the input buffer.
  682.    Returns non-zero if successful, zero if there is
  683.    no space left in the buffer. */
  684. int
  685. rl_unget_char (key)
  686.      int key;
  687. {
  688.   if (ibuffer_space ())
  689.     {
  690.       pop_index--;
  691.       if (pop_index < 0)
  692.     pop_index = ibuffer_len - 1;
  693.       ibuffer[pop_index] = key;
  694.       return (1);
  695.     }
  696.   return (0);
  697. }
  698.  
  699. /* If a character is available to be read, then read it
  700.    and stuff it into IBUFFER.  Otherwise, just return. */
  701. rl_gather_tyi ()
  702. {
  703. #ifdef __GO32__
  704.   char input;
  705.   if (isatty(0))
  706.   {
  707.     int i = rl_getc();
  708.     if (i != EOF)
  709.       rl_stuff_char(i);
  710.   }
  711.   else
  712.     if (kbhit() && ibuffer_space())
  713.       rl_stuff_char(getkey());
  714. #else
  715.   int tty = fileno (in_stream);
  716.   register int tem, result = -1;
  717.   long chars_avail;
  718.   char input;
  719.  
  720. #if defined (FIONREAD)
  721.   result = ioctl (tty, FIONREAD, &chars_avail);
  722. #endif
  723.  
  724.   if (result == -1)
  725.     {
  726.       int flags;
  727.  
  728.       flags = fcntl (tty, F_GETFL, 0);
  729.  
  730.       fcntl (tty, F_SETFL, (flags | O_NDELAY));
  731.       chars_avail = read (tty, &input, 1);
  732.  
  733.       fcntl (tty, F_SETFL, flags);
  734.       if (chars_avail == -1 && errno == EAGAIN)
  735.     return;
  736.     }
  737.  
  738.   /* If there's nothing available, don't waste time trying to read
  739.      something. */
  740.   if (chars_avail == 0)
  741.     return;
  742.  
  743.   tem = ibuffer_space ();
  744.  
  745.   if (chars_avail > tem)
  746.     chars_avail = tem;
  747.  
  748.   /* One cannot read all of the available input.  I can only read a single
  749.      character at a time, or else programs which require input can be
  750.      thwarted.  If the buffer is larger than one character, I lose.
  751.      Damn! */
  752.   if (tem < ibuffer_len)
  753.     chars_avail = 0;
  754.  
  755.   if (result != -1)
  756.     {
  757.       while (chars_avail--)
  758.     rl_stuff_char (rl_getc (in_stream));
  759.     }
  760.   else
  761.     {
  762.       if (chars_avail)
  763.     rl_stuff_char (input);
  764.     }
  765. #endif /* def __GO32__/else */
  766. }
  767.  
  768. static int next_macro_key ();
  769. /* Read a key, including pending input. */
  770. int
  771. rl_read_key ()
  772. {
  773.   int c;
  774.  
  775.   rl_key_sequence_length++;
  776.  
  777.   if (rl_pending_input)
  778.     {
  779.       c = rl_pending_input;
  780.       rl_pending_input = 0;
  781.     }
  782.   else
  783.     {
  784.       /* If input is coming from a macro, then use that. */
  785.       if (c = next_macro_key ())
  786.     return (c);
  787.  
  788.       /* If the user has an event function, then call it periodically. */
  789.       if (rl_event_hook)
  790.     {
  791.       while (rl_event_hook && !rl_get_char (&c))
  792.         {
  793.           (*rl_event_hook) ();
  794.           rl_gather_tyi ();
  795.         }
  796.     }
  797.       else
  798.     {
  799.       if (!rl_get_char (&c))
  800.         c = rl_getc (in_stream);
  801.     }
  802.     }
  803.  
  804.   return (c);
  805. }
  806.  
  807. /* I'm beginning to hate the declaration rules for various compilers. */
  808. static void add_macro_char (), with_macro_input ();
  809.  
  810. /* Do the command associated with KEY in MAP.
  811.    If the associated command is really a keymap, then read
  812.    another key, and dispatch into that map. */
  813. rl_dispatch (key, map)
  814.      register int key;
  815.      Keymap map;
  816. {
  817.  
  818.   if (defining_kbd_macro)
  819.     add_macro_char (key);
  820.  
  821.   if (key > 127 && key < 256)
  822.     {
  823.       if (map[ESC].type == ISKMAP)
  824.     {
  825.       map = (Keymap)map[ESC].function;
  826.       key -= 128;
  827.       rl_dispatch (key, map);
  828.     }
  829.       else
  830.     ding ();
  831.       return;
  832.     }
  833.  
  834.   switch (map[key].type)
  835.     {
  836.     case ISFUNC:
  837.       {
  838.     Function *func = map[key].function;
  839.  
  840.     if (func != (Function *)NULL)
  841.       {
  842.         /* Special case rl_do_lowercase_version (). */
  843.         if (func == rl_do_lowercase_version)
  844.           {
  845.         rl_dispatch (to_lower (key), map);
  846.         return;
  847.           }
  848.  
  849.         (*map[key].function)(rl_numeric_arg * rl_arg_sign, key);
  850.  
  851.         /* If we have input pending, then the last command was a prefix
  852.            command.  Don't change the state of rl_last_func.  Otherwise,
  853.            remember the last command executed in this variable. */
  854.         if (!rl_pending_input)
  855.           rl_last_func = map[key].function;
  856.       }
  857.     else
  858.       {
  859.         rl_abort ();
  860.         return;
  861.       }
  862.       }
  863.       break;
  864.  
  865.     case ISKMAP:
  866.       if (map[key].function != (Function *)NULL)
  867.     {
  868.       int newkey;
  869.  
  870.       rl_key_sequence_length++;
  871.       newkey = rl_read_key ();
  872.       rl_dispatch (newkey, (Keymap)map[key].function);
  873.     }
  874.       else
  875.     {
  876.       rl_abort ();
  877.       return;
  878.     }
  879.       break;
  880.  
  881.     case ISMACR:
  882.       if (map[key].function != (Function *)NULL)
  883.     {
  884.       char *macro;
  885.  
  886.       macro = savestring ((char *)map[key].function);
  887.       with_macro_input (macro);
  888.       return;
  889.     }
  890.       break;
  891.     }
  892. }
  893.  
  894.  
  895. /* **************************************************************** */
  896. /*                                    */
  897. /*            Hacking Keyboard Macros             */
  898. /*                                    */
  899. /* **************************************************************** */
  900.  
  901. /* The currently executing macro string.  If this is non-zero,
  902.    then it is a malloc ()'ed string where input is coming from. */
  903. static char *executing_macro = (char *)NULL;
  904.  
  905. /* The offset in the above string to the next character to be read. */
  906. static int executing_macro_index = 0;
  907.  
  908. /* The current macro string being built.  Characters get stuffed
  909.    in here by add_macro_char (). */
  910. static char *current_macro = (char *)NULL;
  911.  
  912. /* The size of the buffer allocated to current_macro. */
  913. static int current_macro_size = 0;
  914.  
  915. /* The index at which characters are being added to current_macro. */
  916. static int current_macro_index = 0;
  917.  
  918. /* A structure used to save nested macro strings.
  919.    It is a linked list of string/index for each saved macro. */
  920. struct saved_macro {
  921.   struct saved_macro *next;
  922.   char *string;
  923.   int index;
  924. };
  925.  
  926. /* The list of saved macros. */
  927. struct saved_macro *macro_list = (struct saved_macro *)NULL;
  928.  
  929. /* Forward declarations of static functions.  Thank you C. */
  930. static void push_executing_macro (), pop_executing_macro ();
  931.  
  932. /* This one has to be declared earlier in the file. */
  933. /* static void add_macro_char (); */
  934.  
  935. /* Set up to read subsequent input from STRING.
  936.    STRING is free ()'ed when we are done with it. */
  937. static void
  938. with_macro_input (string)
  939.      char *string;
  940. {
  941.   push_executing_macro ();
  942.   executing_macro = string;
  943.   executing_macro_index = 0;
  944. }
  945.  
  946. /* Return the next character available from a macro, or 0 if
  947.    there are no macro characters. */
  948. static int
  949. next_macro_key ()
  950. {
  951.   if (!executing_macro)
  952.     return (0);
  953.  
  954.   if (!executing_macro[executing_macro_index])
  955.     {
  956.       pop_executing_macro ();
  957.       return (next_macro_key ());
  958.     }
  959.  
  960.   return (executing_macro[executing_macro_index++]);
  961. }
  962.  
  963. /* Save the currently executing macro on a stack of saved macros. */
  964. static void
  965. push_executing_macro ()
  966. {
  967.   struct saved_macro *saver;
  968.  
  969.   saver = (struct saved_macro *)xmalloc (sizeof (struct saved_macro));
  970.   saver->next = macro_list;
  971.   saver->index = executing_macro_index;
  972.   saver->string = executing_macro;
  973.  
  974.   macro_list = saver;
  975. }
  976.  
  977. /* Discard the current macro, replacing it with the one
  978.    on the top of the stack of saved macros. */
  979. static void
  980. pop_executing_macro ()
  981. {
  982.   if (executing_macro)
  983.     free (executing_macro);
  984.  
  985.   executing_macro = (char *)NULL;
  986.   executing_macro_index = 0;
  987.  
  988.   if (macro_list)
  989.     {
  990.       struct saved_macro *disposer = macro_list;
  991.       executing_macro = macro_list->string;
  992.       executing_macro_index = macro_list->index;
  993.       macro_list = macro_list->next;
  994.       free (disposer);
  995.     }
  996. }
  997.  
  998. /* Add a character to the macro being built. */
  999. static void
  1000. add_macro_char (c)
  1001.      int c;
  1002. {
  1003.   if (current_macro_index + 1 >= current_macro_size)
  1004.     {
  1005.       if (!current_macro)
  1006.     current_macro = (char *)xmalloc (current_macro_size = 25);
  1007.       else
  1008.     current_macro =
  1009.       (char *)xrealloc (current_macro, current_macro_size += 25);
  1010.     }
  1011.  
  1012.   current_macro[current_macro_index++] = c;
  1013.   current_macro[current_macro_index] = '\0';
  1014. }
  1015.  
  1016. /* Begin defining a keyboard macro.
  1017.    Keystrokes are recorded as they are executed.
  1018.    End the definition with rl_end_kbd_macro ().
  1019.    If a numeric argument was explicitly typed, then append this
  1020.    definition to the end of the existing macro, and start by
  1021.    re-executing the existing macro. */
  1022. rl_start_kbd_macro (ignore1, ignore2)
  1023.      int ignore1, ignore2;
  1024. {
  1025.   if (defining_kbd_macro)
  1026.     rl_abort ();
  1027.  
  1028.   if (rl_explicit_arg)
  1029.     {
  1030.       if (current_macro)
  1031.     with_macro_input (savestring (current_macro));
  1032.     }
  1033.   else
  1034.     current_macro_index = 0;
  1035.  
  1036.   defining_kbd_macro = 1;
  1037. }
  1038.  
  1039. /* Stop defining a keyboard macro.
  1040.    A numeric argument says to execute the macro right now,
  1041.    that many times, counting the definition as the first time. */
  1042. rl_end_kbd_macro (count, ignore)
  1043.      int count, ignore;
  1044. {
  1045.   if (!defining_kbd_macro)
  1046.     rl_abort ();
  1047.  
  1048.   current_macro_index -= (rl_key_sequence_length - 1);
  1049.   current_macro[current_macro_index] = '\0';
  1050.  
  1051.   defining_kbd_macro = 0;
  1052.  
  1053.   rl_call_last_kbd_macro (--count, 0);
  1054. }
  1055.  
  1056. /* Execute the most recently defined keyboard macro.
  1057.    COUNT says how many times to execute it. */
  1058. rl_call_last_kbd_macro (count, ignore)
  1059.      int count, ignore;
  1060. {
  1061.   if (!current_macro)
  1062.     rl_abort ();
  1063.  
  1064.   while (count--)
  1065.     with_macro_input (savestring (current_macro));
  1066. }
  1067.  
  1068.  
  1069. /* **************************************************************** */
  1070. /*                                    */
  1071. /*            Initializations                 */
  1072. /*                                    */
  1073. /* **************************************************************** */
  1074.  
  1075. /* Initliaze readline (and terminal if not already). */
  1076. rl_initialize ()
  1077. {
  1078.   extern char *rl_display_prompt;
  1079.  
  1080.   /* If we have never been called before, initialize the
  1081.      terminal and data structures. */
  1082.   if (!rl_initialized)
  1083.     {
  1084.       readline_initialize_everything ();
  1085.       rl_initialized++;
  1086.     }
  1087.  
  1088.   /* Initalize the current line information. */
  1089.   rl_point = rl_end = 0;
  1090.   the_line = rl_line_buffer;
  1091.   the_line[0] = 0;
  1092.  
  1093.   /* We aren't done yet.  We haven't even gotten started yet! */
  1094.   rl_done = 0;
  1095.  
  1096.   /* Tell the history routines what is going on. */
  1097.   start_using_history ();
  1098.  
  1099.   /* Make the display buffer match the state of the line. */
  1100.   {
  1101.     extern char *rl_display_prompt;
  1102.     extern int forced_display;
  1103.  
  1104.     rl_on_new_line ();
  1105.  
  1106.     rl_display_prompt = rl_prompt ? rl_prompt : "";
  1107.     forced_display = 1;
  1108.   }
  1109.  
  1110.   /* No such function typed yet. */
  1111.   rl_last_func = (Function *)NULL;
  1112.  
  1113.   /* Parsing of key-bindings begins in an enabled state. */
  1114.   parsing_conditionalized_out = 0;
  1115. }
  1116.  
  1117. /* Initialize the entire state of the world. */
  1118. readline_initialize_everything ()
  1119. {
  1120.   /* Find out if we are running in Emacs. */
  1121.   running_in_emacs = getenv ("EMACS");
  1122.  
  1123.   /* Allocate data structures. */
  1124.   if (!rl_line_buffer)
  1125.     rl_line_buffer =
  1126.       (char *)xmalloc (rl_line_buffer_len = DEFAULT_BUFFER_SIZE);
  1127.  
  1128.   /* Initialize the terminal interface. */
  1129.   init_terminal_io ((char *)NULL);
  1130.  
  1131.   /* Bind tty characters to readline functions. */
  1132.   readline_default_bindings ();
  1133.  
  1134.   /* Initialize the function names. */
  1135.   rl_initialize_funmap ();
  1136.  
  1137.   /* Read in the init file. */
  1138.   rl_read_init_file ((char *)NULL);
  1139.  
  1140.   /* If the completion parser's default word break characters haven't
  1141.      been set yet, then do so now. */
  1142.   {
  1143.     extern char *rl_completer_word_break_characters;
  1144.     extern char *rl_basic_word_break_characters;
  1145.  
  1146.     if (rl_completer_word_break_characters == (char *)NULL)
  1147.       rl_completer_word_break_characters = rl_basic_word_break_characters;
  1148.   }
  1149. }
  1150.  
  1151. /* If this system allows us to look at the values of the regular
  1152.    input editing characters, then bind them to their readline
  1153.    equivalents, iff the characters are not bound to keymaps. */
  1154. readline_default_bindings ()
  1155. {
  1156. #ifndef __GO32__
  1157.  
  1158. #if defined (NEW_TTY_DRIVER)
  1159.   struct sgttyb ttybuff;
  1160.   int tty = fileno (rl_instream);
  1161.  
  1162.   if (ioctl (tty, TIOCGETP, &ttybuff) != -1)
  1163.     {
  1164.       int erase, kill;
  1165.  
  1166.       erase = ttybuff.sg_erase;
  1167.       kill  = ttybuff.sg_kill;
  1168.  
  1169.       if (erase != -1 && keymap[erase].type == ISFUNC)
  1170.     keymap[erase].function = rl_rubout;
  1171.  
  1172.       if (kill != -1 && keymap[kill].type == ISFUNC)
  1173.     keymap[kill].function = rl_unix_line_discard;
  1174.     }
  1175.  
  1176. #if defined (TIOCGLTC)
  1177.   {
  1178.     struct ltchars lt;
  1179.  
  1180.     if (ioctl (tty, TIOCGLTC, <) != -1)
  1181.       {
  1182.     int erase, nextc;
  1183.  
  1184.     erase = lt.t_werasc;
  1185.     nextc = lt.t_lnextc;
  1186.  
  1187.     if (erase != -1 && keymap[erase].type == ISFUNC)
  1188.       keymap[erase].function = rl_unix_word_rubout;
  1189.  
  1190.     if (nextc != -1 && keymap[nextc].type == ISFUNC)
  1191.       keymap[nextc].function = rl_quoted_insert;
  1192.       }
  1193.   }
  1194. #endif /* TIOCGLTC */
  1195. #else /* not NEW_TTY_DRIVER */
  1196.  
  1197. #if defined (TERMIOS_TTY_DRIVER)
  1198.   struct termios ttybuff;
  1199. #else
  1200.   struct termio ttybuff;
  1201. #endif /* TERMIOS_TTY_DRIVER */
  1202.   int tty = fileno (rl_instream);
  1203.  
  1204. #if defined (TERMIOS_TTY_DRIVER)
  1205.   if (tcgetattr (tty, &ttybuff) != -1)
  1206. #else
  1207.   if (ioctl (tty, TCGETA, &ttybuff) != -1)
  1208. #endif /* !TERMIOS_TTY_DRIVER */
  1209.     {
  1210.       int erase, kill;
  1211.  
  1212.       erase = ttybuff.c_cc[VERASE];
  1213.       kill = ttybuff.c_cc[VKILL];
  1214.  
  1215.       if (erase != _POSIX_VDISABLE &&
  1216.       keymap[(unsigned char)erase].type == ISFUNC)
  1217.     keymap[(unsigned char)erase].function = rl_rubout;
  1218.  
  1219.       if (kill != _POSIX_VDISABLE &&
  1220.       keymap[(unsigned char)kill].type == ISFUNC)
  1221.     keymap[(unsigned char)kill].function = rl_unix_line_discard;
  1222.  
  1223. #if defined (VLNEXT) && defined (TERMIOS_TTY_DRIVER)
  1224.       {
  1225.     int nextc;
  1226.  
  1227.     nextc = ttybuff.c_cc[VLNEXT];
  1228.  
  1229.     if (nextc != _POSIX_VDISABLE &&
  1230.         keymap[(unsigned char)nextc].type == ISFUNC)
  1231.       keymap[(unsigned char)nextc].function = rl_quoted_insert;
  1232.       }
  1233. #endif /* VLNEXT && TERMIOS_TTY_DRIVER */
  1234.  
  1235. #if defined (VWERASE)
  1236.       {
  1237.     int werase;
  1238.  
  1239.     werase = ttybuff.c_cc[VWERASE];
  1240.  
  1241.     if (werase != _POSIX_VDISABLE &&
  1242.         keymap[(unsigned char)werase].type == ISFUNC)
  1243.       keymap[(unsigned char)werase].function = rl_unix_word_rubout;
  1244.       }
  1245. #endif /* VWERASE */
  1246.     }
  1247. #endif /* !NEW_TTY_DRIVER */
  1248. #endif /* def __GO32__ */
  1249. }
  1250.  
  1251.  
  1252. /* **************************************************************** */
  1253. /*                                    */
  1254. /*            Numeric Arguments                */
  1255. /*                                    */
  1256. /* **************************************************************** */
  1257.  
  1258. /* Handle C-u style numeric args, as well as M--, and M-digits. */
  1259.  
  1260. /* Add the current digit to the argument in progress. */
  1261. rl_digit_argument (ignore, key)
  1262.      int ignore, key;
  1263. {
  1264.   rl_pending_input = key;
  1265.   rl_digit_loop ();
  1266. }
  1267.  
  1268. /* What to do when you abort reading an argument. */
  1269. rl_discard_argument ()
  1270. {
  1271.   ding ();
  1272.   rl_clear_message ();
  1273.   rl_init_argument ();
  1274. }
  1275.  
  1276. /* Create a default argument. */
  1277. rl_init_argument ()
  1278. {
  1279.   rl_numeric_arg = rl_arg_sign = 1;
  1280.   rl_explicit_arg = 0;
  1281. }
  1282.  
  1283. /* C-u, universal argument.  Multiply the current argument by 4.
  1284.    Read a key.  If the key has nothing to do with arguments, then
  1285.    dispatch on it.  If the key is the abort character then abort. */
  1286. rl_universal_argument ()
  1287. {
  1288.   rl_numeric_arg *= 4;
  1289.   rl_digit_loop ();
  1290. }
  1291.  
  1292. rl_digit_loop ()
  1293. {
  1294.   int key, c;
  1295.   while (1)
  1296.     {
  1297.       rl_message ("(arg: %d) ", rl_arg_sign * rl_numeric_arg);
  1298.       key = c = rl_read_key ();
  1299.  
  1300.       if (keymap[c].type == ISFUNC &&
  1301.       keymap[c].function == rl_universal_argument)
  1302.     {
  1303.       rl_numeric_arg *= 4;
  1304.       continue;
  1305.     }
  1306.       c = UNMETA (c);
  1307.       if (numeric (c))
  1308.     {
  1309.       if (rl_explicit_arg)
  1310.         rl_numeric_arg = (rl_numeric_arg * 10) + (c - '0');
  1311.       else
  1312.         rl_numeric_arg = (c - '0');
  1313.       rl_explicit_arg = 1;
  1314.     }
  1315.       else
  1316.     {
  1317.       if (c == '-' && !rl_explicit_arg)
  1318.         {
  1319.           rl_numeric_arg = 1;
  1320.           rl_arg_sign = -1;
  1321.         }
  1322.       else
  1323.         {
  1324.           rl_clear_message ();
  1325.           rl_dispatch (key, keymap);
  1326.           return;
  1327.         }
  1328.     }
  1329.     }
  1330. }
  1331.  
  1332.  
  1333. /* **************************************************************** */
  1334. /*                                    */
  1335. /*            Display stuff                    */
  1336. /*                                    */
  1337. /* **************************************************************** */
  1338.  
  1339. /* This is the stuff that is hard for me.  I never seem to write good
  1340.    display routines in C.  Let's see how I do this time. */
  1341.  
  1342. /* (PWP) Well... Good for a simple line updater, but totally ignores
  1343.    the problems of input lines longer than the screen width.
  1344.  
  1345.    update_line and the code that calls it makes a multiple line,
  1346.    automatically wrapping line update.  Carefull attention needs
  1347.    to be paid to the vertical position variables.
  1348.  
  1349.    handling of terminals with autowrap on (incl. DEC braindamage)
  1350.    could be improved a bit.  Right now I just cheat and decrement
  1351.    screenwidth by one. */
  1352.  
  1353. /* Keep two buffers; one which reflects the current contents of the
  1354.    screen, and the other to draw what we think the new contents should
  1355.    be.  Then compare the buffers, and make whatever changes to the
  1356.    screen itself that we should.  Finally, make the buffer that we
  1357.    just drew into be the one which reflects the current contents of the
  1358.    screen, and place the cursor where it belongs.
  1359.  
  1360.    Commands that want to can fix the display themselves, and then let
  1361.    this function know that the display has been fixed by setting the
  1362.    RL_DISPLAY_FIXED variable.  This is good for efficiency. */
  1363.  
  1364. /* Termcap variables: */
  1365. extern char *term_up, *term_dc, *term_cr;
  1366. extern int screenheight, screenwidth, terminal_can_insert;
  1367.  
  1368. /* What YOU turn on when you have handled all redisplay yourself. */
  1369. int rl_display_fixed = 0;
  1370.  
  1371. /* The visible cursor position.  If you print some text, adjust this. */
  1372. int last_c_pos = 0;
  1373. int last_v_pos = 0;
  1374.  
  1375. /* The last left edge of text that was displayed.  This is used when
  1376.    doing horizontal scrolling.  It shifts in thirds of a screenwidth. */
  1377. static int last_lmargin = 0;
  1378.  
  1379. /* The line display buffers.  One is the line currently displayed on
  1380.    the screen.  The other is the line about to be displayed. */
  1381. static char *visible_line = (char *)NULL;
  1382. static char *invisible_line = (char *)NULL;
  1383.  
  1384. /* Number of lines currently on screen minus 1. */
  1385. int vis_botlin = 0;
  1386.  
  1387. /* A buffer for `modeline' messages. */
  1388. char msg_buf[128];
  1389.  
  1390. /* Non-zero forces the redisplay even if we thought it was unnecessary. */
  1391. int forced_display = 0;
  1392.  
  1393. /* The stuff that gets printed out before the actual text of the line.
  1394.    This is usually pointing to rl_prompt. */
  1395. char *rl_display_prompt = (char *)NULL;
  1396.  
  1397. /* Default and initial buffer size.  Can grow. */
  1398. static int line_size = 1024;
  1399.  
  1400. /* Non-zero means to always use horizontal scrolling in line display. */
  1401. static int horizontal_scroll_mode = 0;
  1402.  
  1403. /* Non-zero means to display an asterisk at the starts of history lines
  1404.    which have been modified. */
  1405. static int mark_modified_lines = 0;
  1406.  
  1407. /* Non-zero means to use a visible bell if one is available rather than
  1408.    simply ringing the terminal bell. */
  1409. static int prefer_visible_bell = 0;
  1410.  
  1411. /* I really disagree with this, but my boss (among others) insists that we
  1412.    support compilers that don't work.  I don't think we are gaining by doing
  1413.    so; what is the advantage in producing better code if we can't use it? */
  1414. /* The following two declarations belong inside the
  1415.    function block, not here. */
  1416. static void move_cursor_relative ();
  1417. static void output_some_chars ();
  1418. static void output_character_function ();
  1419. static int compare_strings ();
  1420.  
  1421. /* Basic redisplay algorithm. */
  1422. rl_redisplay ()
  1423. {
  1424.   register int in, out, c, linenum;
  1425.   register char *line = invisible_line;
  1426.   char *prompt_this_line;
  1427.   int c_pos = 0;
  1428.   int inv_botlin = 0;        /* Number of lines in newly drawn buffer. */
  1429.  
  1430.   extern int readline_echoing_p;
  1431.  
  1432.   if (!readline_echoing_p)
  1433.     return;
  1434.  
  1435.   if (!rl_display_prompt)
  1436.     rl_display_prompt = "";
  1437.  
  1438.   if (!invisible_line)
  1439.     {
  1440.       visible_line = (char *)xmalloc (line_size);
  1441.       invisible_line = (char *)xmalloc (line_size);
  1442.       line = invisible_line;
  1443.       for (in = 0; in < line_size; in++)
  1444.     {
  1445.       visible_line[in] = 0;
  1446.       invisible_line[in] = 1;
  1447.     }
  1448.       rl_on_new_line ();
  1449.     }
  1450.  
  1451.   /* Draw the line into the buffer. */
  1452.   c_pos = -1;
  1453.  
  1454.   /* Mark the line as modified or not.  We only do this for history
  1455.      lines. */
  1456.   out = 0;
  1457.   if (mark_modified_lines && current_history () && rl_undo_list)
  1458.     {
  1459.       line[out++] = '*';
  1460.       line[out] = '\0';
  1461.     }
  1462.  
  1463.   /* If someone thought that the redisplay was handled, but the currently
  1464.      visible line has a different modification state than the one about
  1465.      to become visible, then correct the callers misconception. */
  1466.   if (visible_line[0] != invisible_line[0])
  1467.     rl_display_fixed = 0;
  1468.  
  1469.   prompt_this_line = rindex (rl_display_prompt, '\n');
  1470.   if (!prompt_this_line)
  1471.     prompt_this_line = rl_display_prompt;
  1472.   else
  1473.     {
  1474.       prompt_this_line++;
  1475.       if (forced_display)
  1476.     output_some_chars (rl_display_prompt,
  1477.                prompt_this_line - rl_display_prompt);
  1478.     }
  1479.  
  1480.   strncpy (line + out,  prompt_this_line, strlen (prompt_this_line));
  1481.   out += strlen (prompt_this_line);
  1482.   line[out] = '\0';
  1483.  
  1484.   for (in = 0; in < rl_end; in++)
  1485.     {
  1486.       c = (unsigned char)the_line[in];
  1487.  
  1488.       if (out + 1 >= line_size)
  1489.     {
  1490.       line_size *= 2;
  1491.       visible_line = (char *)xrealloc (visible_line, line_size);
  1492.       invisible_line = (char *)xrealloc (invisible_line, line_size);
  1493.       line = invisible_line;
  1494.     }
  1495.  
  1496.       if (in == rl_point)
  1497.     c_pos = out;
  1498.  
  1499.       if (c > 127)
  1500.     {
  1501.       line[out++] = 'M';
  1502.       line[out++] = '-';
  1503.       line[out++] = c - 128;
  1504.     }
  1505. #define DISPLAY_TABS
  1506. #if defined (DISPLAY_TABS)
  1507.       else if (c == '\t')
  1508.     {
  1509.       register int newout = (out | (int)7) + 1;
  1510.       while (out < newout)
  1511.         line[out++] = ' ';
  1512.     }
  1513. #endif
  1514.       else if (c < 32)
  1515.     {
  1516.       line[out++] = 'C';
  1517.       line[out++] = '-';
  1518.       line[out++] = c + 64;
  1519.     }
  1520.       else if (c == 127)
  1521.     {
  1522.       line[out++] = 'C';
  1523.       line[out++] = '-';
  1524.       line[out++] = '?';
  1525.     }
  1526.       else
  1527.     line[out++] = c;
  1528.     }
  1529.   line[out] = '\0';
  1530.   if (c_pos < 0)
  1531.     c_pos = out;
  1532.  
  1533.   /* PWP: now is when things get a bit hairy.  The visible and invisible
  1534.      line buffers are really multiple lines, which would wrap every
  1535.      (screenwidth - 1) characters.  Go through each in turn, finding
  1536.      the changed region and updating it.  The line order is top to bottom. */
  1537.  
  1538.   /* If we can move the cursor up and down, then use multiple lines,
  1539.      otherwise, let long lines display in a single terminal line, and
  1540.      horizontally scroll it. */
  1541.  
  1542.   if (!horizontal_scroll_mode && term_up && *term_up)
  1543.     {
  1544.       int total_screen_chars = (screenwidth * screenheight);
  1545.  
  1546.       if (!rl_display_fixed || forced_display)
  1547.     {
  1548.       forced_display = 0;
  1549.  
  1550.       /* If we have more than a screenful of material to display, then
  1551.          only display a screenful.  We should display the last screen,
  1552.          not the first.  I'll fix this in a minute. */
  1553.       if (out >= total_screen_chars)
  1554.         out = total_screen_chars - 1;
  1555.  
  1556.       /* Number of screen lines to display. */
  1557.       inv_botlin = out / screenwidth;
  1558.  
  1559.       /* For each line in the buffer, do the updating display. */
  1560.       for (linenum = 0; linenum <= inv_botlin; linenum++)
  1561.         update_line (linenum > vis_botlin ? ""
  1562.              : &visible_line[linenum * screenwidth],
  1563.              &invisible_line[linenum * screenwidth],
  1564.              linenum);
  1565.  
  1566.       /* We may have deleted some lines.  If so, clear the left over
  1567.          blank ones at the bottom out. */
  1568.       if (vis_botlin > inv_botlin)
  1569.         {
  1570.           char *tt;
  1571.           for (; linenum <= vis_botlin; linenum++)
  1572.         {
  1573.           tt = &visible_line[linenum * screenwidth];
  1574.           move_vert (linenum);
  1575.           move_cursor_relative (0, tt);
  1576.           clear_to_eol ((linenum == vis_botlin)?
  1577.                 strlen (tt) : screenwidth);
  1578.         }
  1579.         }
  1580.       vis_botlin = inv_botlin;
  1581.  
  1582.       /* Move the cursor where it should be. */
  1583.       move_vert (c_pos / screenwidth);
  1584.       move_cursor_relative (c_pos % screenwidth,
  1585.                 &invisible_line[(c_pos / screenwidth) * screenwidth]);
  1586.     }
  1587.     }
  1588.   else                /* Do horizontal scrolling. */
  1589.     {
  1590.       int lmargin;
  1591.  
  1592.       /* Always at top line. */
  1593.       last_v_pos = 0;
  1594.  
  1595.       /* If the display position of the cursor would be off the edge
  1596.      of the screen, start the display of this line at an offset that
  1597.      leaves the cursor on the screen. */
  1598.       if (c_pos - last_lmargin > screenwidth - 2)
  1599.     lmargin = (c_pos / (screenwidth / 3) - 2) * (screenwidth / 3);
  1600.       else if (c_pos - last_lmargin < 1)
  1601.     lmargin = ((c_pos - 1) / (screenwidth / 3)) * (screenwidth / 3);
  1602.       else
  1603.     lmargin = last_lmargin;
  1604.  
  1605.       /* If the first character on the screen isn't the first character
  1606.      in the display line, indicate this with a special character. */
  1607.       if (lmargin > 0)
  1608.     line[lmargin] = '<';
  1609.  
  1610.       if (lmargin + screenwidth < out)
  1611.     line[lmargin + screenwidth - 1] = '>';
  1612.  
  1613.       if (!rl_display_fixed || forced_display || lmargin != last_lmargin)
  1614.     {
  1615.       forced_display = 0;
  1616.       update_line (&visible_line[last_lmargin],
  1617.                &invisible_line[lmargin], 0);
  1618.  
  1619.       move_cursor_relative (c_pos - lmargin, &invisible_line[lmargin]);
  1620.       last_lmargin = lmargin;
  1621.     }
  1622.     }
  1623.   fflush (out_stream);
  1624.  
  1625.   /* Swap visible and non-visible lines. */
  1626.   {
  1627.     char *temp = visible_line;
  1628.     visible_line = invisible_line;
  1629.     invisible_line = temp;
  1630.     rl_display_fixed = 0;
  1631.   }
  1632. }
  1633.  
  1634. /* PWP: update_line() is based on finding the middle difference of each
  1635.    line on the screen; vis:
  1636.  
  1637.                  /old first difference
  1638.     /beginning of line   |              /old last same       /old EOL
  1639.     v             v              v                    v
  1640. old:    eddie> Oh, my little gruntle-buggy is to me, as lurgid as
  1641. new:    eddie> Oh, my little buggy says to me, as lurgid as
  1642.     ^             ^        ^               ^
  1643.     \beginning of line   |        \new last same       \new end of line
  1644.                  \new first difference
  1645.  
  1646.    All are character pointers for the sake of speed.  Special cases for
  1647.    no differences, as well as for end of line additions must be handeled.
  1648.  
  1649.    Could be made even smarter, but this works well enough */
  1650. static
  1651. update_line (old, new, current_line)
  1652.      register char *old, *new;
  1653.      int current_line;
  1654. {
  1655.   register char *ofd, *ols, *oe, *nfd, *nls, *ne;
  1656.   int lendiff, wsatend;
  1657.  
  1658.   /* Find first difference. */
  1659.   for (ofd = old, nfd = new;
  1660.        (ofd - old < screenwidth) && *ofd && (*ofd == *nfd);
  1661.        ofd++, nfd++)
  1662.     ;
  1663.  
  1664.   /* Move to the end of the screen line. */
  1665.   for (oe = ofd; ((oe - old) < screenwidth) && *oe; oe++);
  1666.   for (ne = nfd; ((ne - new) < screenwidth) && *ne; ne++);
  1667.  
  1668.   /* If no difference, continue to next line. */
  1669.   if (ofd == oe && nfd == ne)
  1670.     return;
  1671.  
  1672.   wsatend = 1;            /* flag for trailing whitespace */
  1673.   ols = oe - 1;            /* find last same */
  1674.   nls = ne - 1;
  1675.   while ((*ols == *nls) && (ols > ofd) && (nls > nfd))
  1676.     {
  1677.       if (*ols != ' ')
  1678.     wsatend = 0;
  1679.       ols--;
  1680.       nls--;
  1681.     }
  1682.  
  1683.   if (wsatend)
  1684.     {
  1685.       ols = oe;
  1686.       nls = ne;
  1687.     }
  1688.   else if (*ols != *nls)
  1689.     {
  1690.       if (*ols)            /* don't step past the NUL */
  1691.     ols++;
  1692.       if (*nls)
  1693.     nls++;
  1694.     }
  1695.  
  1696.   move_vert (current_line);
  1697.   move_cursor_relative (ofd - old, old);
  1698.  
  1699.   /* if (len (new) > len (old)) */
  1700.   lendiff = (nls - nfd) - (ols - ofd);
  1701.  
  1702.   /* Insert (diff(len(old),len(new)) ch */
  1703.   if (lendiff > 0)
  1704.     {
  1705.       if (terminal_can_insert)
  1706.     {
  1707.       extern char *term_IC;
  1708.  
  1709.       /* Sometimes it is cheaper to print the characters rather than
  1710.          use the terminal's capabilities. */
  1711.       if ((2 * (ne - nfd)) < lendiff && !term_IC)
  1712.         {
  1713.           output_some_chars (nfd, (ne - nfd));
  1714.           last_c_pos += (ne - nfd);
  1715.         }
  1716.       else
  1717.         {
  1718.           if (*ols)
  1719.         {
  1720.           insert_some_chars (nfd, lendiff);
  1721.           last_c_pos += lendiff;
  1722.         }
  1723.           else
  1724.         {
  1725.           /* At the end of a line the characters do not have to
  1726.              be "inserted".  They can just be placed on the screen. */
  1727.           output_some_chars (nfd, lendiff);
  1728.           last_c_pos += lendiff;
  1729.         }
  1730.           /* Copy (new) chars to screen from first diff to last match. */
  1731.           if (((nls - nfd) - lendiff) > 0)
  1732.         {
  1733.           output_some_chars (&nfd[lendiff], ((nls - nfd) - lendiff));
  1734.           last_c_pos += ((nls - nfd) - lendiff);
  1735.         }
  1736.         }
  1737.     }
  1738.       else
  1739.     {        /* cannot insert chars, write to EOL */
  1740.       output_some_chars (nfd, (ne - nfd));
  1741.       last_c_pos += (ne - nfd);
  1742.     }
  1743.     }
  1744.   else                /* Delete characters from line. */
  1745.     {
  1746.       /* If possible and inexpensive to use terminal deletion, then do so. */
  1747.       if (term_dc && (2 * (ne - nfd)) >= (-lendiff))
  1748.     {
  1749.       if (lendiff)
  1750.         delete_chars (-lendiff); /* delete (diff) characters */
  1751.  
  1752.       /* Copy (new) chars to screen from first diff to last match */
  1753.       if ((nls - nfd) > 0)
  1754.         {
  1755.           output_some_chars (nfd, (nls - nfd));
  1756.           last_c_pos += (nls - nfd);
  1757.         }
  1758.     }
  1759.       /* Otherwise, print over the existing material. */
  1760.       else
  1761.     {
  1762.       output_some_chars (nfd, (ne - nfd));
  1763.       last_c_pos += (ne - nfd);
  1764.       clear_to_eol ((oe - old) - (ne - new));
  1765.     }
  1766.     }
  1767. }
  1768.  
  1769. /* (PWP) tell the update routines that we have moved onto a
  1770.    new (empty) line. */
  1771. rl_on_new_line ()
  1772. {
  1773.   if (visible_line)
  1774.     visible_line[0] = '\0';
  1775.  
  1776.   last_c_pos = last_v_pos = 0;
  1777.   vis_botlin = last_lmargin = 0;
  1778. }
  1779.  
  1780. /* Actually update the display, period. */
  1781. rl_forced_update_display ()
  1782. {
  1783.   if (visible_line)
  1784.     {
  1785.       register char *temp = visible_line;
  1786.  
  1787.       while (*temp) *temp++ = '\0';
  1788.     }
  1789.   rl_on_new_line ();
  1790.   forced_display++;
  1791.   rl_redisplay ();
  1792. }
  1793.  
  1794. /* Move the cursor from last_c_pos to NEW, which are buffer indices.
  1795.    DATA is the contents of the screen line of interest; i.e., where
  1796.    the movement is being done. */
  1797. static void
  1798. move_cursor_relative (new, data)
  1799.      int new;
  1800.      char *data;
  1801. {
  1802.   register int i;
  1803.  
  1804.   /* It may be faster to output a CR, and then move forwards instead
  1805.      of moving backwards. */
  1806.   if (new + 1 < last_c_pos - new)
  1807.     {
  1808. #ifdef __MSDOS__
  1809.       putc('\r', out_stream);
  1810. #else
  1811.       tputs (term_cr, 1, output_character_function);
  1812. #endif
  1813.       last_c_pos = 0;
  1814.     }
  1815.  
  1816.   if (last_c_pos == new) return;
  1817.  
  1818.   if (last_c_pos < new)
  1819.     {
  1820.       /* Move the cursor forward.  We do it by printing the command
  1821.      to move the cursor forward if there is one, else print that
  1822.      portion of the output buffer again.  Which is cheaper? */
  1823.  
  1824.       /* The above comment is left here for posterity.  It is faster
  1825.      to print one character (non-control) than to print a control
  1826.      sequence telling the terminal to move forward one character.
  1827.      That kind of control is for people who don't know what the
  1828.      data is underneath the cursor. */
  1829. #if defined (HACK_TERMCAP_MOTION)
  1830.       extern char *term_forward_char;
  1831.  
  1832.       if (term_forward_char)
  1833.     for (i = last_c_pos; i < new; i++)
  1834.       tputs (term_forward_char, 1, output_character_function);
  1835.       else
  1836.     for (i = last_c_pos; i < new; i++)
  1837.       putc (data[i], out_stream);
  1838. #else
  1839.       for (i = last_c_pos; i < new; i++)
  1840.     putc (data[i], out_stream);
  1841. #endif                /* HACK_TERMCAP_MOTION */
  1842.     }
  1843.   else
  1844.     backspace (last_c_pos - new);
  1845.   last_c_pos = new;
  1846. }
  1847.  
  1848. /* PWP: move the cursor up or down. */
  1849. move_vert (to)
  1850.      int to;
  1851. {
  1852.   void output_character_function ();
  1853.   register int delta, i;
  1854.  
  1855.   if (last_v_pos == to) return;
  1856.  
  1857.   if (to > screenheight)
  1858.     return;
  1859.  
  1860. #ifdef __GO32__
  1861.   {
  1862.     int cur_r, cur_c;
  1863.     ScreenGetCursor(&cur_r, &cur_c);
  1864.     ScreenSetCursor(cur_r+to-last_v_pos, cur_c);
  1865.   }
  1866. #else /* __GO32__ */
  1867.   if ((delta = to - last_v_pos) > 0)
  1868.     {
  1869.       for (i = 0; i < delta; i++)
  1870.     putc ('\n', out_stream);
  1871.       tputs (term_cr, 1, output_character_function);
  1872.       last_c_pos = 0;
  1873.     }
  1874.   else
  1875.     {            /* delta < 0 */
  1876.       if (term_up && *term_up)
  1877.     for (i = 0; i < -delta; i++)
  1878.       tputs (term_up, 1, output_character_function);
  1879.     }
  1880. #endif /* __GO32__ */
  1881.   last_v_pos = to;        /* now to is here */
  1882. }
  1883.  
  1884. /* Physically print C on out_stream.  This is for functions which know
  1885.    how to optimize the display. */
  1886. rl_show_char (c)
  1887.      int c;
  1888. {
  1889.   if (c > 127)
  1890.     {
  1891.       fprintf (out_stream, "M-");
  1892.       c -= 128;
  1893.     }
  1894.  
  1895. #if defined (DISPLAY_TABS)
  1896.   if (c < 32 && c != '\t')
  1897. #else
  1898.   if (c < 32)
  1899. #endif
  1900.     {
  1901.  
  1902.       c += 64;
  1903.     }
  1904.  
  1905.   putc (c, out_stream);
  1906.   fflush (out_stream);
  1907. }
  1908.  
  1909. #if defined (DISPLAY_TABS)
  1910. int
  1911. rl_character_len (c, pos)
  1912.      register int c, pos;
  1913. {
  1914.   if (c < ' ' || c > 126)
  1915.     {
  1916.       if (c == '\t')
  1917.     return (((pos | (int)7) + 1) - pos);
  1918.       else
  1919.     return (3);
  1920.     }
  1921.   else
  1922.     return (1);
  1923. }
  1924. #else
  1925. int
  1926. rl_character_len (c)
  1927.      int c;
  1928. {
  1929.   if (c < ' ' || c > 126)
  1930.     return (3);
  1931.   else
  1932.     return (1);
  1933. }
  1934. #endif  /* DISPLAY_TAB */
  1935.  
  1936. /* How to print things in the "echo-area".  The prompt is treated as a
  1937.    mini-modeline. */
  1938. rl_message (string, arg1, arg2)
  1939.      char *string;
  1940. {
  1941.   sprintf (msg_buf, string, arg1, arg2);
  1942.   rl_display_prompt = msg_buf;
  1943.   rl_redisplay ();
  1944. }
  1945.  
  1946. /* How to clear things from the "echo-area". */
  1947. rl_clear_message ()
  1948. {
  1949.   rl_display_prompt = rl_prompt;
  1950.   rl_redisplay ();
  1951. }
  1952.  
  1953. /* **************************************************************** */
  1954. /*                                    */
  1955. /*            Terminal and Termcap                */
  1956. /*                                    */
  1957. /* **************************************************************** */
  1958.  
  1959. static char *term_buffer = (char *)NULL;
  1960. static char *term_string_buffer = (char *)NULL;
  1961.  
  1962. /* Non-zero means this terminal can't really do anything. */
  1963. int dumb_term = 0;
  1964.  
  1965. char PC;
  1966. char *BC, *UP;
  1967.  
  1968. /* Some strings to control terminal actions.  These are output by tputs (). */
  1969. char *term_goto, *term_clreol, *term_cr, *term_clrpag, *term_backspace;
  1970.  
  1971. int screenwidth, screenheight;
  1972.  
  1973. /* Non-zero if we determine that the terminal can do character insertion. */
  1974. int terminal_can_insert = 0;
  1975.  
  1976. /* How to insert characters. */
  1977. char *term_im, *term_ei, *term_ic, *term_ip, *term_IC;
  1978.  
  1979. /* How to delete characters. */
  1980. char *term_dc, *term_DC;
  1981.  
  1982. #if defined (HACK_TERMCAP_MOTION)
  1983. char *term_forward_char;
  1984. #endif  /* HACK_TERMCAP_MOTION */
  1985.  
  1986. /* How to go up a line. */
  1987. char *term_up;
  1988.  
  1989. /* A visible bell, if the terminal can be made to flash the screen. */
  1990. char *visible_bell;
  1991.  
  1992. /* Re-initialize the terminal considering that the TERM/TERMCAP variable
  1993.    has changed. */
  1994. rl_reset_terminal (terminal_name)
  1995.      char *terminal_name;
  1996. {
  1997.   init_terminal_io (terminal_name);
  1998. }
  1999.  
  2000. init_terminal_io (terminal_name)
  2001.      char *terminal_name;
  2002. {
  2003. #ifdef __GO32__
  2004.   screenwidth = ScreenCols();
  2005.   screenheight = ScreenRows();
  2006.   term_cr = "\r";
  2007.   term_im = term_ei = term_ic = term_IC = (char *)NULL;
  2008.   term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  2009. #if defined (HACK_TERMCAP_MOTION)
  2010.       term_forward_char = (char *)NULL;
  2011. #endif
  2012.   terminal_can_insert = 0;
  2013.   return;
  2014. #else
  2015.   extern char *tgetstr ();
  2016.   char *term, *buffer;
  2017. #if defined (TIOCGWINSZ)
  2018.   struct winsize window_size;
  2019. #endif
  2020.   int tty;
  2021.  
  2022.   term = terminal_name ? terminal_name : getenv ("TERM");
  2023.  
  2024.   if (!term_string_buffer)
  2025.     term_string_buffer = (char *)xmalloc (2048);
  2026.  
  2027.   if (!term_buffer)
  2028.     term_buffer = (char *)xmalloc (2048);
  2029.  
  2030.   buffer = term_string_buffer;
  2031.  
  2032.   term_clrpag = term_cr = term_clreol = (char *)NULL;
  2033.  
  2034.   if (!term)
  2035.     term = "dumb";
  2036.  
  2037.   if (tgetent (term_buffer, term) < 0)
  2038.     {
  2039.       dumb_term = 1;
  2040.       screenwidth = 79;
  2041.       screenheight = 24;
  2042.       term_cr = "\r";
  2043.       term_im = term_ei = term_ic = term_IC = (char *)NULL;
  2044.       term_up = term_dc = term_DC = visible_bell = (char *)NULL;
  2045. #if defined (HACK_TERMCAP_MOTION)
  2046.       term_forward_char = (char *)NULL;
  2047. #endif
  2048.       terminal_can_insert = 0;
  2049.       return;
  2050.     }
  2051.  
  2052.   BC = tgetstr ("pc", &buffer);
  2053.   PC = buffer ? *buffer : 0;
  2054.  
  2055.   term_backspace = tgetstr ("le", &buffer);
  2056.  
  2057.   term_cr = tgetstr ("cr", &buffer);
  2058.   term_clreol = tgetstr ("ce", &buffer);
  2059.   term_clrpag = tgetstr ("cl", &buffer);
  2060.  
  2061.   if (!term_cr)
  2062.     term_cr =  "\r";
  2063.  
  2064. #if defined (HACK_TERMCAP_MOTION)
  2065.   term_forward_char = tgetstr ("nd", &buffer);
  2066. #endif  /* HACK_TERMCAP_MOTION */
  2067.  
  2068.   if (rl_instream)
  2069.     tty = fileno (rl_instream);
  2070.   else
  2071.     tty = 0;
  2072.  
  2073.   screenwidth = screenheight = 0;
  2074. #if defined (TIOCGWINSZ)
  2075.   if (ioctl (tty, TIOCGWINSZ, &window_size) == 0)
  2076.     {
  2077.       screenwidth = (int) window_size.ws_col;
  2078.       screenheight = (int) window_size.ws_row;
  2079.     }
  2080. #endif
  2081.  
  2082.   if (screenwidth <= 0 || screenheight <= 0)
  2083.     {
  2084.       screenwidth = tgetnum ("co");
  2085.       screenheight = tgetnum ("li");
  2086.     }
  2087.  
  2088.   screenwidth--;
  2089.  
  2090.   if (screenwidth <= 0)
  2091.     screenwidth = 79;
  2092.  
  2093.   if (screenheight <= 0)
  2094.     screenheight = 24;
  2095.  
  2096.   term_im = tgetstr ("im", &buffer);
  2097.   term_ei = tgetstr ("ei", &buffer);
  2098.   term_IC = tgetstr ("IC", &buffer);
  2099.   term_ic = tgetstr ("ic", &buffer);
  2100.  
  2101.   /* "An application program can assume that the terminal can do
  2102.       character insertion if *any one of* the capabilities `IC',
  2103.       `im', `ic' or `ip' is provided."  But we can't do anything if
  2104.       only `ip' is provided, so... */
  2105.   terminal_can_insert = (term_IC || term_im || term_ic);
  2106.  
  2107.   term_up = tgetstr ("up", &buffer);
  2108.   term_dc = tgetstr ("dc", &buffer);
  2109.   term_DC = tgetstr ("DC", &buffer);
  2110.  
  2111.   visible_bell = tgetstr ("vb", &buffer);
  2112. #endif /* !__GO32__ */
  2113. }
  2114.  
  2115. /* A function for the use of tputs () */
  2116. static void
  2117. output_character_function (c)
  2118.      int c;
  2119. {
  2120.   putc (c, out_stream);
  2121. }
  2122.  
  2123. /* Write COUNT characters from STRING to the output stream. */
  2124. static void
  2125. output_some_chars (string, count)
  2126.      char *string;
  2127.      int count;
  2128. {
  2129.   fwrite (string, 1, count, out_stream);
  2130. }
  2131.  
  2132. /* Delete COUNT characters from the display line. */
  2133. static
  2134. delete_chars (count)
  2135.      int count;
  2136. {
  2137. #ifdef __GO32__
  2138.   int r, c, w;
  2139.   ScreenGetCursor(&r, &c);
  2140.   w = ScreenCols();
  2141.   memcpy(ScreenPrimary+r*w+c, ScreenPrimary+r*w+c+count, w-c-count);
  2142.   memset(ScreenPrimary+r*w+w-count, 0, count*2);
  2143. #else /* __GO32__ */
  2144.   if (count > screenwidth)
  2145.     return;
  2146.  
  2147.   if (term_DC && *term_DC)
  2148.     {
  2149.       char *tgoto (), *buffer;
  2150.       buffer = tgoto (term_DC, 0, count);
  2151.       tputs (buffer, 1, output_character_function);
  2152.     }
  2153.   else
  2154.     {
  2155.       if (term_dc && *term_dc)
  2156.     while (count--)
  2157.       tputs (term_dc, 1, output_character_function);
  2158.     }
  2159. #endif /* __GO32__ */
  2160. }
  2161.  
  2162. /* Insert COUNT characters from STRING to the output stream. */
  2163. static
  2164. insert_some_chars (string, count)
  2165.      char *string;
  2166.      int count;
  2167. {
  2168. #ifdef __GO32__
  2169.   int r, c, w;
  2170.   ScreenGetCursor(&r, &c);
  2171.   w = ScreenCols();
  2172.   memcpy(ScreenPrimary+r*w+c+count, ScreenPrimary+r*w+c, w-c-count);
  2173.   /* Print the text. */
  2174.   output_some_chars (string, count);
  2175. #else /* __GO32__ */
  2176.   /* If IC is defined, then we do not have to "enter" insert mode. */
  2177.   if (term_IC)
  2178.     {
  2179.       char *tgoto (), *buffer;
  2180.       buffer = tgoto (term_IC, 0, count);
  2181.       tputs (buffer, 1, output_character_function);
  2182.       output_some_chars (string, count);
  2183.     }
  2184.   else
  2185.     {
  2186.       register int i;
  2187.  
  2188.       /* If we have to turn on insert-mode, then do so. */
  2189.       if (term_im && *term_im)
  2190.     tputs (term_im, 1, output_character_function);
  2191.  
  2192.       /* If there is a special command for inserting characters, then
  2193.      use that first to open up the space. */
  2194.       if (term_ic && *term_ic)
  2195.     {
  2196.       for (i = count; i--; )
  2197.         tputs (term_ic, 1, output_character_function);
  2198.     }
  2199.  
  2200.       /* Print the text. */
  2201.       output_some_chars (string, count);
  2202.  
  2203.       /* If there is a string to turn off insert mode, we had best use
  2204.      it now. */
  2205.       if (term_ei && *term_ei)
  2206.     tputs (term_ei, 1, output_character_function);
  2207.     }
  2208. #endif /* __GO32__ */
  2209. }
  2210.  
  2211. /* Move the cursor back. */
  2212. backspace (count)
  2213.      int count;
  2214. {
  2215.   register int i;
  2216.  
  2217. #ifndef __GO32__
  2218.   if (term_backspace)
  2219.     for (i = 0; i < count; i++)
  2220.       tputs (term_backspace, 1, output_character_function);
  2221.   else
  2222. #endif /* !__GO32__ */
  2223.     for (i = 0; i < count; i++)
  2224.       putc ('\b', out_stream);
  2225. }
  2226.  
  2227. /* Move to the start of the next line. */
  2228. crlf ()
  2229. {
  2230. #if defined (NEW_TTY_DRIVER)
  2231.   tputs (term_cr, 1, output_character_function);
  2232. #endif /* NEW_TTY_DRIVER */
  2233.   putc ('\n', out_stream);
  2234. }
  2235.  
  2236. /* Clear to the end of the line.  COUNT is the minimum
  2237.    number of character spaces to clear, */
  2238. clear_to_eol (count)
  2239.      int count;
  2240. {
  2241. #ifndef __GO32__
  2242.   if (term_clreol)
  2243.     {
  2244.       tputs (term_clreol, 1, output_character_function);
  2245.     }
  2246.   else
  2247. #endif /* !__GO32__ */
  2248.     {
  2249.       register int i;
  2250.  
  2251.       /* Do one more character space. */
  2252.       count++;
  2253.  
  2254.       for (i = 0; i < count; i++)
  2255.     putc (' ', out_stream);
  2256.  
  2257.       backspace (count);
  2258.     }
  2259. }
  2260.  
  2261.  
  2262. /* **************************************************************** */
  2263. /*                                    */
  2264. /*              Saving and Restoring the TTY                */
  2265. /*                                    */
  2266. /* **************************************************************** */
  2267.  
  2268. /* Non-zero means that the terminal is in a prepped state. */
  2269. static int terminal_prepped = 0;
  2270.  
  2271. #if defined (NEW_TTY_DRIVER)
  2272.  
  2273. /* Standard flags, including ECHO. */
  2274. static int original_tty_flags = 0;
  2275.  
  2276. /* Local mode flags, like LPASS8. */
  2277. static int local_mode_flags = 0;
  2278.  
  2279. /* Terminal characters.  This has C-s and C-q in it. */
  2280. static struct tchars original_tchars;
  2281.  
  2282. /* Local special characters.  This has the interrupt characters in it. */
  2283. #if defined (TIOCGLTC)
  2284. static struct ltchars original_ltchars;
  2285. #endif
  2286.  
  2287. /* We use this to get and set the tty_flags. */
  2288. static struct sgttyb the_ttybuff;
  2289.  
  2290. /* Put the terminal in CBREAK mode so that we can detect key presses. */
  2291. static void
  2292. rl_prep_terminal ()
  2293. {
  2294. #ifndef __GO32__
  2295.   int tty = fileno (rl_instream);
  2296. #if defined (HAVE_BSD_SIGNALS)
  2297.   int oldmask;
  2298. #endif /* HAVE_BSD_SIGNALS */
  2299.  
  2300.   if (terminal_prepped)
  2301.     return;
  2302.  
  2303.   oldmask = sigblock (sigmask (SIGINT));
  2304.  
  2305.   /* We always get the latest tty values.  Maybe stty changed them. */
  2306.   ioctl (tty, TIOCGETP, &the_ttybuff);
  2307.   original_tty_flags = the_ttybuff.sg_flags;
  2308.  
  2309.   readline_echoing_p = (original_tty_flags & ECHO);
  2310.  
  2311. #if defined (TIOCLGET)
  2312.   ioctl (tty, TIOCLGET, &local_mode_flags);
  2313. #endif
  2314.  
  2315. #if !defined (ANYP)
  2316. #  define ANYP (EVENP | ODDP)
  2317. #endif
  2318.  
  2319.   /* If this terminal doesn't care how the 8th bit is used,
  2320.      then we can use it for the meta-key.  We check by seeing
  2321.      if BOTH odd and even parity are allowed. */
  2322.   if (the_ttybuff.sg_flags & ANYP)
  2323.     {
  2324. #if defined (PASS8)
  2325.       the_ttybuff.sg_flags |= PASS8;
  2326. #endif
  2327.  
  2328.       /* Hack on local mode flags if we can. */
  2329. #if defined (TIOCLGET) && defined (LPASS8)
  2330.       {
  2331.     int flags;
  2332.     flags = local_mode_flags | LPASS8;
  2333.     ioctl (tty, TIOCLSET, &flags);
  2334.       }
  2335. #endif /* TIOCLGET && LPASS8 */
  2336.     }
  2337.  
  2338. #if defined (TIOCGETC)
  2339.   {
  2340.     struct tchars temp;
  2341.  
  2342.     ioctl (tty, TIOCGETC, &original_tchars);
  2343.     temp = original_tchars;
  2344.  
  2345. #if defined (USE_XON_XOFF)
  2346.     /* Get rid of C-s and C-q.
  2347.        We remember the value of startc (C-q) so that if the terminal is in
  2348.        xoff state, the user can xon it by pressing that character. */
  2349.     xon_char = temp.t_startc;
  2350.     temp.t_stopc = -1;
  2351.     temp.t_startc = -1;
  2352.  
  2353.     /* If there is an XON character, bind it to restart the output. */
  2354.     if (xon_char != -1)
  2355.       rl_bind_key (xon_char, rl_restart_output);
  2356. #endif /* USE_XON_XOFF */
  2357.  
  2358.     /* If there is an EOF char, bind eof_char to it. */
  2359.     if (temp.t_eofc != -1)
  2360.       eof_char = temp.t_eofc;
  2361.  
  2362. #if defined (NO_KILL_INTR)
  2363.     /* Get rid of C-\ and C-c. */
  2364.     temp.t_intrc = temp.t_quitc = -1;
  2365. #endif /* NO_KILL_INTR */
  2366.  
  2367.     ioctl (tty, TIOCSETC, &temp);
  2368.   }
  2369. #endif /* TIOCGETC */
  2370.  
  2371. #if defined (TIOCGLTC)
  2372.   {
  2373.     struct ltchars temp;
  2374.  
  2375.     ioctl (tty, TIOCGLTC, &original_ltchars);
  2376.     temp = original_ltchars;
  2377.  
  2378.     /* Make the interrupt keys go away.  Just enough to make people
  2379.        happy. */
  2380.     temp.t_dsuspc = -1;    /* C-y */
  2381.     temp.t_lnextc = -1;    /* C-v */
  2382.  
  2383.     ioctl (tty, TIOCSLTC, &temp);
  2384.   }
  2385. #endif /* TIOCGLTC */
  2386.  
  2387.   the_ttybuff.sg_flags &= ~(ECHO | CRMOD);
  2388.   the_ttybuff.sg_flags |= CBREAK;
  2389.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2390.  
  2391.   terminal_prepped = 1;
  2392.  
  2393. #if defined (HAVE_BSD_SIGNALS)
  2394.   sigsetmask (oldmask);
  2395. #endif
  2396. #endif /* !__GO32__ */
  2397. }
  2398.  
  2399. /* Restore the terminal to its original state. */
  2400. static void
  2401. rl_deprep_terminal ()
  2402. {
  2403. #ifndef __GO32__
  2404.   int tty = fileno (rl_instream);
  2405. #if defined (HAVE_BSD_SIGNALS)
  2406.   int oldmask;
  2407. #endif
  2408.  
  2409.   if (!terminal_prepped)
  2410.     return;
  2411.  
  2412.   oldmask = sigblock (sigmask (SIGINT));
  2413.  
  2414.   the_ttybuff.sg_flags = original_tty_flags;
  2415.   ioctl (tty, TIOCSETN, &the_ttybuff);
  2416.   readline_echoing_p = 1;
  2417.  
  2418. #if defined (TIOCLGET)
  2419.   ioctl (tty, TIOCLSET, &local_mode_flags);
  2420. #endif
  2421.  
  2422. #if defined (TIOCSLTC)
  2423.   ioctl (tty, TIOCSLTC, &original_ltchars);
  2424. #endif
  2425.  
  2426. #if defined (TIOCSETC)
  2427.   ioctl (tty, TIOCSETC, &original_tchars);
  2428. #endif
  2429.   terminal_prepped = 0;
  2430.  
  2431. #if defined (HAVE_BSD_SIGNALS)
  2432.   sigsetmask (oldmask);
  2433. #endif
  2434. #endif /* !__GO32 */
  2435. }
  2436.  
  2437. #else  /* !defined (NEW_TTY_DRIVER) */
  2438.  
  2439. #if !defined (VMIN)
  2440. #define VMIN VEOF
  2441. #endif
  2442.  
  2443. #if !defined (VTIME)
  2444. #define VTIME VEOL
  2445. #endif
  2446.  
  2447. #ifndef __GO32__
  2448. #if defined (TERMIOS_TTY_DRIVER)
  2449. static struct termios otio;
  2450. #else
  2451. static struct termio otio;
  2452. #endif /* !TERMIOS_TTY_DRIVER */
  2453. #endif /* __GO32__ */
  2454.  
  2455. static void
  2456. rl_prep_terminal ()
  2457. {
  2458. #ifndef __GO32__
  2459.   int tty = fileno (rl_instream);
  2460. #if defined (TERMIOS_TTY_DRIVER)
  2461.   struct termios tio;
  2462. #else
  2463.   struct termio tio;
  2464. #endif /* !TERMIOS_TTY_DRIVER */
  2465.  
  2466. #if defined (HAVE_POSIX_SIGNALS)
  2467.   sigset_t set, oset;
  2468. #else
  2469. #  if defined (HAVE_BSD_SIGNALS)
  2470.   int oldmask;
  2471. #  endif /* HAVE_BSD_SIGNALS */
  2472. #endif /* !HAVE_POSIX_SIGNALS */
  2473.  
  2474.   if (terminal_prepped)
  2475.     return;
  2476.  
  2477.   /* Try to keep this function from being INTerrupted.  We can do it
  2478.      on POSIX and systems with BSD-like signal handling. */
  2479. #if defined (HAVE_POSIX_SIGNALS)
  2480.   sigemptyset (&set);
  2481.   sigaddset (&set, SIGINT);
  2482.   sigprocmask (SIG_BLOCK, &set, &oset);
  2483. #else /* !HAVE_POSIX_SIGNALS */
  2484. #  if defined (HAVE_BSD_SIGNALS)
  2485.   oldmask = sigblock (sigmask (SIGINT));
  2486. #  endif /* HAVE_BSD_SIGNALS */
  2487. #endif /* !HAVE_POSIX_SIGNALS */
  2488.  
  2489. #if defined (TERMIOS_TTY_DRIVER)
  2490.   tcgetattr (tty, &tio);
  2491. #else
  2492.   ioctl (tty, TCGETA, &tio);
  2493. #endif /* !TERMIOS_TTY_DRIVER */
  2494.  
  2495.   otio = tio;
  2496.  
  2497.   readline_echoing_p = (tio.c_lflag & ECHO);
  2498.  
  2499.   tio.c_lflag &= ~(ICANON|ECHO);
  2500.  
  2501.   if (otio.c_cc[VEOF] != _POSIX_VDISABLE)
  2502.     eof_char = otio.c_cc[VEOF];
  2503.  
  2504. #if defined (USE_XON_XOFF)
  2505. #if defined (IXANY)
  2506.   tio.c_iflag &= ~(IXON|IXOFF|IXANY);
  2507. #else
  2508.   /* `strict' Posix systems do not define IXANY. */
  2509.   tio.c_iflag &= ~(IXON|IXOFF);
  2510. #endif /* IXANY */
  2511. #endif /* USE_XON_XOFF */
  2512.  
  2513.   /* Only turn this off if we are using all 8 bits. */
  2514.   /* |ISTRIP|INPCK */
  2515.   tio.c_iflag &= ~(ISTRIP | INPCK);
  2516.  
  2517.   /* Make sure we differentiate between CR and NL on input. */
  2518.   tio.c_iflag &= ~(ICRNL | INLCR);
  2519.  
  2520. #if !defined (HANDLE_SIGNALS)
  2521.   tio.c_lflag &= ~ISIG;
  2522. #else
  2523.   tio.c_lflag |= ISIG;
  2524. #endif
  2525.  
  2526.   tio.c_cc[VMIN] = 1;
  2527.   tio.c_cc[VTIME] = 0;
  2528.  
  2529.   /* Turn off characters that we need on Posix systems with job control,
  2530.      just to be sure.  This includes ^Y and ^V.  This should not really
  2531.      be necessary.  */
  2532. #if defined (TERMIOS_TTY_DRIVER) && defined (_POSIX_JOB_CONTROL)
  2533.  
  2534. #if defined (VLNEXT)
  2535.   tio.c_cc[VLNEXT] = _POSIX_VDISABLE;
  2536. #endif
  2537.  
  2538. #if defined (VDSUSP)
  2539.   tio.c_cc[VDSUSP] = _POSIX_VDISABLE;
  2540. #endif
  2541.  
  2542. #endif /* POSIX && JOB_CONTROL */
  2543.  
  2544. #if defined (TERMIOS_TTY_DRIVER)
  2545.   tcsetattr (tty, TCSADRAIN, &tio);
  2546.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2547. #else
  2548.   ioctl (tty, TCSETAW, &tio);
  2549.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2550. #endif /* !TERMIOS_TTY_DRIVER */
  2551.  
  2552.   terminal_prepped = 1;
  2553.  
  2554. #if defined (HAVE_POSIX_SIGNALS)
  2555.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2556. #else
  2557. #  if defined (HAVE_BSD_SIGNALS)
  2558.   sigsetmask (oldmask);
  2559. #  endif /* HAVE_BSD_SIGNALS */
  2560. #endif /* !HAVE_POSIX_SIGNALS */
  2561. #endif /* !__GO32__ */
  2562. }
  2563.  
  2564. static void
  2565. rl_deprep_terminal ()
  2566. {
  2567. #ifndef __GO32__ 
  2568.   int tty = fileno (rl_instream);
  2569.  
  2570.   /* Try to keep this function from being INTerrupted.  We can do it
  2571.      on POSIX and systems with BSD-like signal handling. */
  2572. #if defined (HAVE_POSIX_SIGNALS)
  2573.   sigset_t set, oset;
  2574. #else /* !HAVE_POSIX_SIGNALS */
  2575. #  if defined (HAVE_BSD_SIGNALS)
  2576.   int oldmask;
  2577. #  endif /* HAVE_BSD_SIGNALS */
  2578. #endif /* !HAVE_POSIX_SIGNALS */
  2579.  
  2580.   if (!terminal_prepped)
  2581.     return;
  2582.  
  2583. #if defined (HAVE_POSIX_SIGNALS)
  2584.   sigemptyset (&set);
  2585.   sigaddset (&set, SIGINT);
  2586.   sigprocmask (SIG_BLOCK, &set, &oset);
  2587. #else /* !HAVE_POSIX_SIGNALS */
  2588. #  if defined (HAVE_BSD_SIGNALS)
  2589.   oldmask = sigblock (sigmask (SIGINT));
  2590. #  endif /* HAVE_BSD_SIGNALS */
  2591. #endif /* !HAVE_POSIX_SIGNALS */
  2592.  
  2593. #if defined (TERMIOS_TTY_DRIVER)
  2594.   tcsetattr (tty, TCSADRAIN, &otio);
  2595.   tcflow (tty, TCOON);        /* Simulate a ^Q. */
  2596. #else /* TERMIOS_TTY_DRIVER */
  2597.   ioctl (tty, TCSETAW, &otio);
  2598.   ioctl (tty, TCXONC, 1);    /* Simulate a ^Q. */
  2599. #endif /* !TERMIOS_TTY_DRIVER */
  2600.  
  2601.   terminal_prepped = 0;
  2602.  
  2603. #if defined (HAVE_POSIX_SIGNALS)
  2604.   sigprocmask (SIG_SETMASK, &oset, (sigset_t *)NULL);
  2605. #else /* !HAVE_POSIX_SIGNALS */
  2606. #  if defined (HAVE_BSD_SIGNALS)
  2607.   sigsetmask (oldmask);
  2608. #  endif /* HAVE_BSD_SIGNALS */
  2609. #endif /* !HAVE_POSIX_SIGNALS */
  2610. #endif /* !__GO32__ */
  2611. }
  2612. #endif  /* NEW_TTY_DRIVER */
  2613.  
  2614.  
  2615. /* **************************************************************** */
  2616. /*                                    */
  2617. /*            Utility Functions                */
  2618. /*                                    */
  2619. /* **************************************************************** */
  2620.  
  2621. /* Return 0 if C is not a member of the class of characters that belong
  2622.    in words, or 1 if it is. */
  2623.  
  2624. int allow_pathname_alphabetic_chars = 0;
  2625. char *pathname_alphabetic_chars = "/-_=~.#$";
  2626.  
  2627. int
  2628. alphabetic (c)
  2629.      int c;
  2630. {
  2631.   if (pure_alphabetic (c) || (numeric (c)))
  2632.     return (1);
  2633.  
  2634.   if (allow_pathname_alphabetic_chars)
  2635.     return ((int)rindex (pathname_alphabetic_chars, c));
  2636.   else
  2637.     return (0);
  2638. }
  2639.  
  2640. /* Return non-zero if C is a numeric character. */
  2641. int
  2642. numeric (c)
  2643.      int c;
  2644. {
  2645.   return (c >= '0' && c <= '9');
  2646. }
  2647.  
  2648. /* Ring the terminal bell. */
  2649. int
  2650. ding ()
  2651. {
  2652.   if (readline_echoing_p)
  2653.     {
  2654. #ifndef __GO32__
  2655.       if (prefer_visible_bell && visible_bell)
  2656.     tputs (visible_bell, 1, output_character_function);
  2657.       else
  2658. #endif /* !__GO32__ */
  2659.     {
  2660.       fprintf (stderr, "\007");
  2661.       fflush (stderr);
  2662.     }
  2663.     }
  2664.   return (-1);
  2665. }
  2666.  
  2667. /* How to abort things. */
  2668. rl_abort ()
  2669. {
  2670.   ding ();
  2671.   rl_clear_message ();
  2672.   rl_init_argument ();
  2673.   rl_pending_input = 0;
  2674.  
  2675.   defining_kbd_macro = 0;
  2676.   while (executing_macro)
  2677.     pop_executing_macro ();
  2678.  
  2679.   rl_last_func = (Function *)NULL;
  2680.   longjmp (readline_top_level, 1);
  2681. }
  2682.  
  2683. /* Return a copy of the string between FROM and TO.
  2684.    FROM is inclusive, TO is not. */
  2685. #if defined (sun) /* Yes, that's right, some crufty function in sunview is
  2686.              called rl_copy (). */
  2687. static
  2688. #endif
  2689. char *
  2690. rl_copy (from, to)
  2691.      int from, to;
  2692. {
  2693.   register int length;
  2694.   char *copy;
  2695.  
  2696.   /* Fix it if the caller is confused. */
  2697.   if (from > to)
  2698.     {
  2699.       int t = from;
  2700.       from = to;
  2701.       to = t;
  2702.     }
  2703.  
  2704.   length = to - from;
  2705.   copy = (char *)xmalloc (1 + length);
  2706.   strncpy (copy, the_line + from, length);
  2707.   copy[length] = '\0';
  2708.   return (copy);
  2709. }
  2710.  
  2711. /* Increase the size of RL_LINE_BUFFER until it has enough space to hold
  2712.    LEN characters. */
  2713. void
  2714. rl_extend_line_buffer (len)
  2715.      int len;
  2716. {
  2717.   while (len >= rl_line_buffer_len)
  2718.     rl_line_buffer =
  2719.       (char *)xrealloc
  2720.     (rl_line_buffer, rl_line_buffer_len += DEFAULT_BUFFER_SIZE);
  2721.  
  2722.   the_line = rl_line_buffer;
  2723. }
  2724.  
  2725.  
  2726. /* **************************************************************** */
  2727. /*                                    */
  2728. /*            Insert and Delete                */
  2729. /*                                    */
  2730. /* **************************************************************** */
  2731.  
  2732. /* Insert a string of text into the line at point.  This is the only
  2733.    way that you should do insertion.  rl_insert () calls this
  2734.    function. */
  2735. rl_insert_text (string)
  2736.      char *string;
  2737. {
  2738.   extern int doing_an_undo;
  2739.   register int i, l = strlen (string);
  2740.  
  2741.   if (rl_end + l >= rl_line_buffer_len)
  2742.     rl_extend_line_buffer (rl_end + l);
  2743.  
  2744.   for (i = rl_end; i >= rl_point; i--)
  2745.     the_line[i + l] = the_line[i];
  2746.   strncpy (the_line + rl_point, string, l);
  2747.  
  2748.   /* Remember how to undo this if we aren't undoing something. */
  2749.   if (!doing_an_undo)
  2750.     {
  2751.       /* If possible and desirable, concatenate the undos. */
  2752.       if ((strlen (string) == 1) &&
  2753.       rl_undo_list &&
  2754.       (rl_undo_list->what == UNDO_INSERT) &&
  2755.       (rl_undo_list->end == rl_point) &&
  2756.       (rl_undo_list->end - rl_undo_list->start < 20))
  2757.     rl_undo_list->end++;
  2758.       else
  2759.     rl_add_undo (UNDO_INSERT, rl_point, rl_point + l, (char *)NULL);
  2760.     }
  2761.   rl_point += l;
  2762.   rl_end += l;
  2763.   the_line[rl_end] = '\0';
  2764. }
  2765.  
  2766. /* Delete the string between FROM and TO.  FROM is
  2767.    inclusive, TO is not. */
  2768. rl_delete_text (from, to)
  2769.      int from, to;
  2770. {
  2771.   extern int doing_an_undo;
  2772.   register char *text;
  2773.  
  2774.   /* Fix it if the caller is confused. */
  2775.   if (from > to)
  2776.     {
  2777.       int t = from;
  2778.       from = to;
  2779.       to = t;
  2780.     }
  2781.   text = rl_copy (from, to);
  2782.   strncpy (the_line + from, the_line + to, rl_end - to);
  2783.  
  2784.   /* Remember how to undo this delete. */
  2785.   if (!doing_an_undo)
  2786.     rl_add_undo (UNDO_DELETE, from, to, text);
  2787.   else
  2788.     free (text);
  2789.  
  2790.   rl_end -= (to - from);
  2791.   the_line[rl_end] = '\0';
  2792. }
  2793.  
  2794.  
  2795. /* **************************************************************** */
  2796. /*                                    */
  2797. /*            Readline character functions            */
  2798. /*                                    */
  2799. /* **************************************************************** */
  2800.  
  2801. /* This is not a gap editor, just a stupid line input routine.  No hair
  2802.    is involved in writing any of the functions, and none should be. */
  2803.  
  2804. /* Note that:
  2805.  
  2806.    rl_end is the place in the string that we would place '\0';
  2807.    i.e., it is always safe to place '\0' there.
  2808.  
  2809.    rl_point is the place in the string where the cursor is.  Sometimes
  2810.    this is the same as rl_end.
  2811.  
  2812.    Any command that is called interactively receives two arguments.
  2813.    The first is a count: the numeric arg pased to this command.
  2814.    The second is the key which invoked this command.
  2815. */
  2816.  
  2817.  
  2818. /* **************************************************************** */
  2819. /*                                    */
  2820. /*            Movement Commands                */
  2821. /*                                    */
  2822. /* **************************************************************** */
  2823.  
  2824. /* Note that if you `optimize' the display for these functions, you cannot
  2825.    use said functions in other functions which do not do optimizing display.
  2826.    I.e., you will have to update the data base for rl_redisplay, and you
  2827.    might as well let rl_redisplay do that job. */
  2828.  
  2829. /* Move forward COUNT characters. */
  2830. rl_forward (count)
  2831.      int count;
  2832. {
  2833.   if (count < 0)
  2834.     rl_backward (-count);
  2835.   else
  2836.     while (count)
  2837.       {
  2838. #if defined (VI_MODE)
  2839.     if (rl_point == (rl_end - (rl_editing_mode == vi_mode)))
  2840. #else
  2841.     if (rl_point == rl_end)
  2842. #endif /* VI_MODE */
  2843.       {
  2844.         ding ();
  2845.         return;
  2846.       }
  2847.     else
  2848.       rl_point++;
  2849.     --count;
  2850.       }
  2851. }
  2852.  
  2853. /* Move backward COUNT characters. */
  2854. rl_backward (count)
  2855.      int count;
  2856. {
  2857.   if (count < 0)
  2858.     rl_forward (-count);
  2859.   else
  2860.     while (count)
  2861.       {
  2862.     if (!rl_point)
  2863.       {
  2864.         ding ();
  2865.         return;
  2866.       }
  2867.     else
  2868.       --rl_point;
  2869.     --count;
  2870.       }
  2871. }
  2872.  
  2873. /* Move to the beginning of the line. */
  2874. rl_beg_of_line ()
  2875. {
  2876.   rl_point = 0;
  2877. }
  2878.  
  2879. /* Move to the end of the line. */
  2880. rl_end_of_line ()
  2881. {
  2882.   rl_point = rl_end;
  2883. }
  2884.  
  2885. /* Move forward a word.  We do what Emacs does. */
  2886. rl_forward_word (count)
  2887.      int count;
  2888. {
  2889.   int c;
  2890.  
  2891.   if (count < 0)
  2892.     {
  2893.       rl_backward_word (-count);
  2894.       return;
  2895.     }
  2896.  
  2897.   while (count)
  2898.     {
  2899.       if (rl_point == rl_end)
  2900.     return;
  2901.  
  2902.       /* If we are not in a word, move forward until we are in one.
  2903.      Then, move forward until we hit a non-alphabetic character. */
  2904.       c = the_line[rl_point];
  2905.       if (!alphabetic (c))
  2906.     {
  2907.       while (++rl_point < rl_end)
  2908.         {
  2909.           c = the_line[rl_point];
  2910.           if (alphabetic (c)) break;
  2911.         }
  2912.     }
  2913.       if (rl_point == rl_end) return;
  2914.       while (++rl_point < rl_end)
  2915.     {
  2916.       c = the_line[rl_point];
  2917.       if (!alphabetic (c)) break;
  2918.     }
  2919.       --count;
  2920.     }
  2921. }
  2922.  
  2923. /* Move backward a word.  We do what Emacs does. */
  2924. rl_backward_word (count)
  2925.      int count;
  2926. {
  2927.   int c;
  2928.  
  2929.   if (count < 0)
  2930.     {
  2931.       rl_forward_word (-count);
  2932.       return;
  2933.     }
  2934.  
  2935.   while (count)
  2936.     {
  2937.       if (!rl_point)
  2938.     return;
  2939.  
  2940.       /* Like rl_forward_word (), except that we look at the characters
  2941.      just before point. */
  2942.  
  2943.       c = the_line[rl_point - 1];
  2944.       if (!alphabetic (c))
  2945.     {
  2946.       while (--rl_point)
  2947.         {
  2948.           c = the_line[rl_point - 1];
  2949.           if (alphabetic (c)) break;
  2950.         }
  2951.     }
  2952.  
  2953.       while (rl_point)
  2954.     {
  2955.       c = the_line[rl_point - 1];
  2956.       if (!alphabetic (c))
  2957.         break;
  2958.       else --rl_point;
  2959.     }
  2960.       --count;
  2961.     }
  2962. }
  2963.  
  2964. /* Clear the current line.  Numeric argument to C-l does this. */
  2965. rl_refresh_line ()
  2966. {
  2967.   int curr_line = last_c_pos / screenwidth;
  2968.   extern char *term_clreol;
  2969.  
  2970.   move_vert(curr_line);
  2971.   move_cursor_relative (0, the_line);   /* XXX is this right */
  2972.  
  2973. #ifdef __GO32__
  2974.   {
  2975.   int r, c, w;
  2976.   ScreenGetCursor(&r, &c);
  2977.   w = ScreenCols();
  2978.   memset(ScreenPrimary+r*w+c, 0, (w-c)*2);
  2979.   }
  2980. #else /* __GO32__ */
  2981.   if (term_clreol)
  2982.     tputs (term_clreol, 1, output_character_function);
  2983. #endif /* __GO32__/else */
  2984.  
  2985.   rl_forced_update_display ();
  2986.   rl_display_fixed = 1;
  2987. }
  2988.  
  2989. /* C-l typed to a line without quoting clears the screen, and then reprints
  2990.    the prompt and the current input line.  Given a numeric arg, redraw only
  2991.    the current line. */
  2992. rl_clear_screen ()
  2993. {
  2994.   extern char *term_clrpag;
  2995.  
  2996.   if (rl_explicit_arg)
  2997.     {
  2998.       rl_refresh_line ();
  2999.       return;
  3000.     }
  3001.  
  3002. #ifndef __GO32__
  3003.   if (term_clrpag)
  3004.     tputs (term_clrpag, 1, output_character_function);
  3005.   else
  3006. #endif /* !__GO32__ */
  3007.     crlf ();
  3008.  
  3009.   rl_forced_update_display ();
  3010.   rl_display_fixed = 1;
  3011. }
  3012.  
  3013. rl_arrow_keys (count, c)
  3014.      int count, c;
  3015. {
  3016.   int ch;
  3017.  
  3018.   ch = rl_read_key ();
  3019.  
  3020.   switch (to_upper (ch))
  3021.     {
  3022.     case 'A':
  3023.       rl_get_previous_history (count);
  3024.       break;
  3025.  
  3026.     case 'B':
  3027.       rl_get_next_history (count);
  3028.       break;
  3029.  
  3030.     case 'C':
  3031.       rl_forward (count);
  3032.       break;
  3033.  
  3034.     case 'D':
  3035.       rl_backward (count);
  3036.       break;
  3037.  
  3038.     default:
  3039.       ding ();
  3040.     }
  3041. }
  3042.  
  3043.  
  3044. /* **************************************************************** */
  3045. /*                                    */
  3046. /*            Text commands                    */
  3047. /*                                    */
  3048. /* **************************************************************** */
  3049.  
  3050. /* Insert the character C at the current location, moving point forward. */
  3051. rl_insert (count, c)
  3052.      int count, c;
  3053. {
  3054.   register int i;
  3055.   char *string;
  3056.  
  3057.   if (count <= 0)
  3058.     return;
  3059.  
  3060.   /* If we can optimize, then do it.  But don't let people crash
  3061.      readline because of extra large arguments. */
  3062.   if (count > 1 && count < 1024)
  3063.     {
  3064.       string = (char *)alloca (1 + count);
  3065.  
  3066.       for (i = 0; i < count; i++)
  3067.     string[i] = c;
  3068.  
  3069.       string[i] = '\0';
  3070.       rl_insert_text (string);
  3071.       return;
  3072.     }
  3073.  
  3074.   if (count > 1024)
  3075.     {
  3076.       int decreaser;
  3077.  
  3078.       string = (char *)alloca (1024 + 1);
  3079.  
  3080.       for (i = 0; i < 1024; i++)
  3081.     string[i] = c;
  3082.  
  3083.       while (count)
  3084.     {
  3085.       decreaser = (count > 1024 ? 1024 : count);
  3086.       string[decreaser] = '\0';
  3087.       rl_insert_text (string);
  3088.       count -= decreaser;
  3089.     }
  3090.       return;
  3091.     }
  3092.  
  3093.   /* We are inserting a single character.
  3094.      If there is pending input, then make a string of all of the
  3095.      pending characters that are bound to rl_insert, and insert
  3096.      them all. */
  3097.   if (any_typein)
  3098.     {
  3099.       int key = 0, t;
  3100.  
  3101.       i = 0;
  3102.       string = (char *)alloca (ibuffer_len + 1);
  3103.       string[i++] = c;
  3104.  
  3105.       while ((t = rl_get_char (&key)) &&
  3106.          (keymap[key].type == ISFUNC &&
  3107.           keymap[key].function == rl_insert))
  3108.     string[i++] = key;
  3109.  
  3110.       if (t)
  3111.     rl_unget_char (key);
  3112.  
  3113.       string[i] = '\0';
  3114.       rl_insert_text (string);
  3115.       return;
  3116.     }
  3117.   else
  3118.     {
  3119.       /* Inserting a single character. */
  3120.       string = (char *)alloca (2);
  3121.  
  3122.       string[1] = '\0';
  3123.       string[0] = c;
  3124.       rl_insert_text (string);
  3125.     }
  3126. }
  3127.  
  3128. /* Insert the next typed character verbatim. */
  3129. rl_quoted_insert (count)
  3130.      int count;
  3131. {
  3132.   int c = rl_read_key ();
  3133.   rl_insert (count, c);
  3134. }
  3135.  
  3136. /* Insert a tab character. */
  3137. rl_tab_insert (count)
  3138.      int count;
  3139. {
  3140.   rl_insert (count, '\t');
  3141. }
  3142.  
  3143. /* What to do when a NEWLINE is pressed.  We accept the whole line.
  3144.    KEY is the key that invoked this command.  I guess it could have
  3145.    meaning in the future. */
  3146. rl_newline (count, key)
  3147.      int count, key;
  3148. {
  3149.  
  3150.   rl_done = 1;
  3151.  
  3152. #if defined (VI_MODE)
  3153.   {
  3154.     extern int vi_doing_insert;
  3155.     if (vi_doing_insert)
  3156.       {
  3157.     rl_end_undo_group ();
  3158.     vi_doing_insert = 0;
  3159.       }
  3160.   }
  3161. #endif /* VI_MODE */
  3162.  
  3163.   if (readline_echoing_p)
  3164.     {
  3165.       move_vert (vis_botlin);
  3166.       vis_botlin = 0;
  3167.       crlf ();
  3168.       fflush (out_stream);
  3169.       rl_display_fixed++;
  3170.     }
  3171. }
  3172.  
  3173. rl_clean_up_for_exit ()
  3174. {
  3175.   if (readline_echoing_p)
  3176.     {
  3177.       move_vert (vis_botlin);
  3178.       vis_botlin = 0;
  3179.       fflush (out_stream);
  3180.       rl_restart_output ();
  3181.     }
  3182. }
  3183.  
  3184. /* What to do for some uppercase characters, like meta characters,
  3185.    and some characters appearing in emacs_ctlx_keymap.  This function
  3186.    is just a stub, you bind keys to it and the code in rl_dispatch ()
  3187.    is special cased. */
  3188. rl_do_lowercase_version (ignore1, ignore2)
  3189.      int ignore1, ignore2;
  3190. {
  3191. }
  3192.  
  3193. /* Rubout the character behind point. */
  3194. rl_rubout (count)
  3195.      int count;
  3196. {
  3197.   if (count < 0)
  3198.     {
  3199.       rl_delete (-count);
  3200.       return;
  3201.     }
  3202.  
  3203.   if (!rl_point)
  3204.     {
  3205.       ding ();
  3206.       return;
  3207.     }
  3208.  
  3209.   if (count > 1)
  3210.     {
  3211.       int orig_point = rl_point;
  3212.       rl_backward (count);
  3213.       rl_kill_text (orig_point, rl_point);
  3214.     }
  3215.   else
  3216.     {
  3217.       int c = the_line[--rl_point];
  3218.       rl_delete_text (rl_point, rl_point + 1);
  3219.  
  3220.       if (rl_point == rl_end && alphabetic (c) && last_c_pos)
  3221.     {
  3222.       backspace (1);
  3223.       putc (' ', out_stream);
  3224.       backspace (1);
  3225.       last_c_pos--;
  3226.       visible_line[last_c_pos] = '\0';
  3227.       rl_display_fixed++;
  3228.     }
  3229.     }
  3230. }
  3231.  
  3232. /* Delete the character under the cursor.  Given a numeric argument,
  3233.    kill that many characters instead. */
  3234. rl_delete (count, invoking_key)
  3235.      int count, invoking_key;
  3236. {
  3237.   if (count < 0)
  3238.     {
  3239.       rl_rubout (-count);
  3240.       return;
  3241.     }
  3242.  
  3243.   if (rl_point == rl_end)
  3244.     {
  3245.       ding ();
  3246.       return;
  3247.     }
  3248.  
  3249.   if (count > 1)
  3250.     {
  3251.       int orig_point = rl_point;
  3252.       rl_forward (count);
  3253.       rl_kill_text (orig_point, rl_point);
  3254.       rl_point = orig_point;
  3255.     }
  3256.   else
  3257.     rl_delete_text (rl_point, rl_point + 1);
  3258. }
  3259.  
  3260.  
  3261. /* **************************************************************** */
  3262. /*                                    */
  3263. /*            Kill commands                    */
  3264. /*                                    */
  3265. /* **************************************************************** */
  3266.  
  3267. /* The next two functions mimic unix line editing behaviour, except they
  3268.    save the deleted text on the kill ring.  This is safer than not saving
  3269.    it, and since we have a ring, nobody should get screwed. */
  3270.  
  3271. /* This does what C-w does in Unix.  We can't prevent people from
  3272.    using behaviour that they expect. */
  3273. rl_unix_word_rubout ()
  3274. {
  3275.   if (!rl_point) ding ();
  3276.   else {
  3277.     int orig_point = rl_point;
  3278.     while (rl_point && whitespace (the_line[rl_point - 1]))
  3279.       rl_point--;
  3280.     while (rl_point && !whitespace (the_line[rl_point - 1]))
  3281.       rl_point--;
  3282.     rl_kill_text (rl_point, orig_point);
  3283.   }
  3284. }
  3285.  
  3286. /* Here is C-u doing what Unix does.  You don't *have* to use these
  3287.    key-bindings.  We have a choice of killing the entire line, or
  3288.    killing from where we are to the start of the line.  We choose the
  3289.    latter, because if you are a Unix weenie, then you haven't backspaced
  3290.    into the line at all, and if you aren't, then you know what you are
  3291.    doing. */
  3292. rl_unix_line_discard ()
  3293. {
  3294.   if (!rl_point) ding ();
  3295.   else {
  3296.     rl_kill_text (rl_point, 0);
  3297.     rl_point = 0;
  3298.   }
  3299. }
  3300.  
  3301.  
  3302.  
  3303. /* **************************************************************** */
  3304. /*                                    */
  3305. /*            Commands For Typos                */
  3306. /*                                    */
  3307. /* **************************************************************** */
  3308.  
  3309. /* Random and interesting things in here.  */
  3310.  
  3311. /* **************************************************************** */
  3312. /*                                    */
  3313. /*            Changing Case                    */
  3314. /*                                    */
  3315. /* **************************************************************** */
  3316.  
  3317. /* The three kinds of things that we know how to do. */
  3318. #define UpCase 1
  3319. #define DownCase 2
  3320. #define CapCase 3
  3321.  
  3322. /* Uppercase the word at point. */
  3323. rl_upcase_word (count)
  3324.      int count;
  3325. {
  3326.   rl_change_case (count, UpCase);
  3327. }
  3328.  
  3329. /* Lowercase the word at point. */
  3330. rl_downcase_word (count)
  3331.      int count;
  3332. {
  3333.   rl_change_case (count, DownCase);
  3334. }
  3335.  
  3336. /* Upcase the first letter, downcase the rest. */
  3337. rl_capitalize_word (count)
  3338.      int count;
  3339. {
  3340.   rl_change_case (count, CapCase);
  3341. }
  3342.  
  3343. /* The meaty function.
  3344.    Change the case of COUNT words, performing OP on them.
  3345.    OP is one of UpCase, DownCase, or CapCase.
  3346.    If a negative argument is given, leave point where it started,
  3347.    otherwise, leave it where it moves to. */
  3348. rl_change_case (count, op)
  3349.      int count, op;
  3350. {
  3351.   register int start = rl_point, end;
  3352.   int state = 0;
  3353.  
  3354.   rl_forward_word (count);
  3355.   end = rl_point;
  3356.  
  3357.   if (count < 0)
  3358.     {
  3359.       int temp = start;
  3360.       start = end;
  3361.       end = temp;
  3362.     }
  3363.  
  3364.   /* We are going to modify some text, so let's prepare to undo it. */
  3365.   rl_modifying (start, end);
  3366.  
  3367.   for (; start < end; start++)
  3368.     {
  3369.       switch (op)
  3370.     {
  3371.     case UpCase:
  3372.       the_line[start] = to_upper (the_line[start]);
  3373.       break;
  3374.  
  3375.     case DownCase:
  3376.       the_line[start] = to_lower (the_line[start]);
  3377.       break;
  3378.  
  3379.     case CapCase:
  3380.       if (state == 0)
  3381.         {
  3382.           the_line[start] = to_upper (the_line[start]);
  3383.           state = 1;
  3384.         }
  3385.       else
  3386.         {
  3387.           the_line[start] = to_lower (the_line[start]);
  3388.         }
  3389.       if (!pure_alphabetic (the_line[start]))
  3390.         state = 0;
  3391.       break;
  3392.  
  3393.     default:
  3394.       abort ();
  3395.     }
  3396.     }
  3397.   rl_point = end;
  3398. }
  3399.  
  3400. /* **************************************************************** */
  3401. /*                                    */
  3402. /*            Transposition                    */
  3403. /*                                    */
  3404. /* **************************************************************** */
  3405.  
  3406. /* Transpose the words at point. */
  3407. rl_transpose_words (count)
  3408.      int count;
  3409. {
  3410.   char *word1, *word2;
  3411.   int w1_beg, w1_end, w2_beg, w2_end;
  3412.   int orig_point = rl_point;
  3413.  
  3414.   if (!count) return;
  3415.  
  3416.   /* Find the two words. */
  3417.   rl_forward_word (count);
  3418.   w2_end = rl_point;
  3419.   rl_backward_word (1);
  3420.   w2_beg = rl_point;
  3421.   rl_backward_word (count);
  3422.   w1_beg = rl_point;
  3423.   rl_forward_word (1);
  3424.   w1_end = rl_point;
  3425.  
  3426.   /* Do some check to make sure that there really are two words. */
  3427.   if ((w1_beg == w2_beg) || (w2_beg < w1_end))
  3428.     {
  3429.       ding ();
  3430.       rl_point = orig_point;
  3431.       return;
  3432.     }
  3433.  
  3434.   /* Get the text of the words. */
  3435.   word1 = rl_copy (w1_beg, w1_end);
  3436.   word2 = rl_copy (w2_beg, w2_end);
  3437.  
  3438.   /* We are about to do many insertions and deletions.  Remember them
  3439.      as one operation. */
  3440.   rl_begin_undo_group ();
  3441.  
  3442.   /* Do the stuff at word2 first, so that we don't have to worry
  3443.      about word1 moving. */
  3444.   rl_point = w2_beg;
  3445.   rl_delete_text (w2_beg, w2_end);
  3446.   rl_insert_text (word1);
  3447.  
  3448.   rl_point = w1_beg;
  3449.   rl_delete_text (w1_beg, w1_end);
  3450.   rl_insert_text (word2);
  3451.  
  3452.   /* This is exactly correct since the text before this point has not
  3453.      changed in length. */
  3454.   rl_point = w2_end;
  3455.  
  3456.   /* I think that does it. */
  3457.   rl_end_undo_group ();
  3458.   free (word1); free (word2);
  3459. }
  3460.  
  3461. /* Transpose the characters at point.  If point is at the end of the line,
  3462.    then transpose the characters before point. */
  3463. rl_transpose_chars (count)
  3464.      int count;
  3465. {
  3466.   if (!count)
  3467.     return;
  3468.  
  3469.   if (!rl_point || rl_end < 2) {
  3470.     ding ();
  3471.     return;
  3472.   }
  3473.  
  3474.   while (count)
  3475.     {
  3476.       if (rl_point == rl_end)
  3477.     {
  3478.       int t = the_line[rl_point - 1];
  3479.  
  3480.       the_line[rl_point - 1] = the_line[rl_point - 2];
  3481.       the_line[rl_point - 2] = t;
  3482.     }
  3483.       else
  3484.     {
  3485.       int t = the_line[rl_point];
  3486.  
  3487.       the_line[rl_point] = the_line[rl_point - 1];
  3488.       the_line[rl_point - 1] = t;
  3489.  
  3490.       if (count < 0 && rl_point)
  3491.         rl_point--;
  3492.       else
  3493.         rl_point++;
  3494.     }
  3495.  
  3496.       if (count < 0)
  3497.     count++;
  3498.       else
  3499.     count--;
  3500.     }
  3501. }
  3502.  
  3503.  
  3504. /* **************************************************************** */
  3505. /*                                    */
  3506. /*            Bogus Flow Control                  */
  3507. /*                                    */
  3508. /* **************************************************************** */
  3509.  
  3510. rl_restart_output (count, key)
  3511.      int count, key;
  3512. {
  3513.   int fildes = fileno (rl_outstream);
  3514. #if defined (TIOCSTART)
  3515. #if defined (apollo)
  3516.   ioctl (&fildes, TIOCSTART, 0);
  3517. #else
  3518.   ioctl (fildes, TIOCSTART, 0);
  3519. #endif /* apollo */
  3520.  
  3521. #else
  3522. #  if defined (TERMIOS_TTY_DRIVER)
  3523.         tcflow (fildes, TCOON);
  3524. #  else
  3525. #    if defined (TCXONC)
  3526.         ioctl (fildes, TCXONC, TCOON);
  3527. #    endif /* TCXONC */
  3528. #  endif /* !TERMIOS_TTY_DRIVER */
  3529. #endif /* TIOCSTART */
  3530. }
  3531.  
  3532. rl_stop_output (count, key)
  3533.      int count, key;
  3534. {
  3535.   int fildes = fileno (rl_instream);
  3536.  
  3537. #if defined (TIOCSTOP)
  3538. # if defined (apollo)
  3539.   ioctl (&fildes, TIOCSTOP, 0);
  3540. # else
  3541.   ioctl (fildes, TIOCSTOP, 0);
  3542. # endif /* apollo */
  3543. #else
  3544. # if defined (TERMIOS_TTY_DRIVER)
  3545.   tcflow (fildes, TCOOFF);
  3546. # else
  3547. #   if defined (TCXONC)
  3548.   ioctl (fildes, TCXONC, TCOON);
  3549. #   endif /* TCXONC */
  3550. # endif /* !TERMIOS_TTY_DRIVER */
  3551. #endif /* TIOCSTOP */
  3552. }
  3553.  
  3554. /* **************************************************************** */
  3555. /*                                    */
  3556. /*    Completion matching, from readline's point of view.        */
  3557. /*                                    */
  3558. /* **************************************************************** */
  3559.  
  3560. /* Pointer to the generator function for completion_matches ().
  3561.    NULL means to use filename_entry_function (), the default filename
  3562.    completer. */
  3563. Function *rl_completion_entry_function = (Function *)NULL;
  3564.  
  3565. /* Pointer to alternative function to create matches.
  3566.    Function is called with TEXT, START, and END.
  3567.    START and END are indices in RL_LINE_BUFFER saying what the boundaries
  3568.    of TEXT are.
  3569.    If this function exists and returns NULL then call the value of
  3570.    rl_completion_entry_function to try to match, otherwise use the
  3571.    array of strings returned. */
  3572. Function *rl_attempted_completion_function = (Function *)NULL;
  3573.  
  3574. /* Local variable states what happened during the last completion attempt. */
  3575. static int completion_changed_buffer = 0;
  3576.  
  3577. /* Complete the word at or before point.  You have supplied the function
  3578.    that does the initial simple matching selection algorithm (see
  3579.    completion_matches ()).  The default is to do filename completion. */
  3580.  
  3581. rl_complete (ignore, invoking_key)
  3582.      int ignore, invoking_key;
  3583. {
  3584.   if (rl_last_func == rl_complete && !completion_changed_buffer)
  3585.     rl_complete_internal ('?');
  3586.   else
  3587.     rl_complete_internal (TAB);
  3588. }
  3589.  
  3590. /* List the possible completions.  See description of rl_complete (). */
  3591. rl_possible_completions ()
  3592. {
  3593.   rl_complete_internal ('?');
  3594. }
  3595.  
  3596. /* The user must press "y" or "n". Non-zero return means "y" pressed. */
  3597. get_y_or_n ()
  3598. {
  3599.   int c;
  3600.  loop:
  3601.   c = rl_read_key ();
  3602.   if (c == 'y' || c == 'Y') return (1);
  3603.   if (c == 'n' || c == 'N') return (0);
  3604.   if (c == ABORT_CHAR) rl_abort ();
  3605.   ding (); goto loop;
  3606. }
  3607.  
  3608. /* Up to this many items will be displayed in response to a
  3609.    possible-completions call.  After that, we ask the user if
  3610.    she is sure she wants to see them all. */
  3611. int rl_completion_query_items = 100;
  3612.  
  3613. /* The basic list of characters that signal a break between words for the
  3614.    completer routine.  The contents of this variable is what breaks words
  3615.    in the shell, i.e. " \t\n\"\\'`@$><=" */
  3616. char *rl_basic_word_break_characters = " \t\n\"\\'`@$><=;|&{(";
  3617.  
  3618. /* The list of characters that signal a break between words for
  3619.    rl_complete_internal.  The default list is the contents of
  3620.    rl_basic_word_break_characters.  */
  3621. char *rl_completer_word_break_characters = (char *)NULL;
  3622.  
  3623. /* List of characters that are word break characters, but should be left
  3624.    in TEXT when it is passed to the completion function.  The shell uses
  3625.    this to help determine what kind of completing to do. */
  3626. char *rl_special_prefixes = (char *)NULL;
  3627.  
  3628. /* If non-zero, then disallow duplicates in the matches. */
  3629. int rl_ignore_completion_duplicates = 1;
  3630.  
  3631. /* Non-zero means that the results of the matches are to be treated
  3632.    as filenames.  This is ALWAYS zero on entry, and can only be changed
  3633.    within a completion entry finder function. */
  3634. int rl_filename_completion_desired = 0;
  3635.  
  3636. /* This function, if defined, is called by the completer when real
  3637.    filename completion is done, after all the matching names have been
  3638.    generated. It is passed a (char**) known as matches in the code below.
  3639.    It consists of a NULL-terminated array of pointers to potential
  3640.    matching strings.  The 1st element (matches[0]) is the maximal
  3641.    substring that is common to all matches. This function can re-arrange
  3642.    the list of matches as required, but all elements of the array must be
  3643.    free()'d if they are deleted. The main intent of this function is
  3644.    to implement FIGNORE a la SunOS csh. */
  3645. Function *rl_ignore_some_completions_function = (Function *)NULL;
  3646.  
  3647. /* Complete the word at or before point.
  3648.    WHAT_TO_DO says what to do with the completion.
  3649.    `?' means list the possible completions.
  3650.    TAB means do standard completion.
  3651.    `*' means insert all of the possible completions. */
  3652. rl_complete_internal (what_to_do)
  3653.      int what_to_do;
  3654. {
  3655.   char *filename_completion_function ();
  3656.   char **completion_matches (), **matches;
  3657.   Function *our_func;
  3658.   int start, end, delimiter = 0;
  3659.   char *text, *saved_line_buffer;
  3660.  
  3661.   if (the_line)
  3662.     saved_line_buffer = savestring (the_line);
  3663.   else
  3664.     saved_line_buffer = (char *)NULL;
  3665.  
  3666.   if (rl_completion_entry_function)
  3667.     our_func = rl_completion_entry_function;
  3668.   else
  3669.     our_func = (int (*)())filename_completion_function;
  3670.  
  3671.   /* Only the completion entry function can change this. */
  3672.   rl_filename_completion_desired = 0;
  3673.  
  3674.   /* We now look backwards for the start of a filename/variable word. */
  3675.   end = rl_point;
  3676.  
  3677.   if (rl_point)
  3678.     {
  3679.       while (--rl_point &&
  3680.          !rindex (rl_completer_word_break_characters, the_line[rl_point]));
  3681.  
  3682.       /* If we are at a word break, then advance past it. */
  3683.       if (rindex (rl_completer_word_break_characters, the_line[rl_point]))
  3684.     {
  3685.       /* If the character that caused the word break was a quoting
  3686.          character, then remember it as the delimiter. */
  3687.       if (rindex ("\"'", the_line[rl_point]) && (end - rl_point) > 1)
  3688.         delimiter = the_line[rl_point];
  3689.  
  3690.       /* If the character isn't needed to determine something special
  3691.          about what kind of completion to perform, then advance past it. */
  3692.  
  3693.       if (!rl_special_prefixes ||
  3694.           !rindex (rl_special_prefixes, the_line[rl_point]))
  3695.         rl_point++;
  3696.     }
  3697.     }
  3698.  
  3699.   start = rl_point;
  3700.   rl_point = end;
  3701.   text = rl_copy (start, end);
  3702.  
  3703.   /* If the user wants to TRY to complete, but then wants to give
  3704.      up and use the default completion function, they set the
  3705.      variable rl_attempted_completion_function. */
  3706.   if (rl_attempted_completion_function)
  3707.     {
  3708.       matches =
  3709.     (char **)(*rl_attempted_completion_function) (text, start, end);
  3710.  
  3711.       if (matches)
  3712.     {
  3713.       our_func = (Function *)NULL;
  3714.       goto after_usual_completion;
  3715.     }
  3716.     }
  3717.  
  3718.   matches = completion_matches (text, our_func);
  3719.  
  3720.  after_usual_completion:
  3721.   free (text);
  3722.  
  3723.   if (!matches)
  3724.     ding ();
  3725.   else
  3726.     {
  3727.       register int i;
  3728.  
  3729.     some_matches:
  3730.  
  3731.       /* It seems to me that in all the cases we handle we would like
  3732.      to ignore duplicate possiblilities.  Scan for the text to
  3733.      insert being identical to the other completions. */
  3734.       if (rl_ignore_completion_duplicates)
  3735.     {
  3736.       char *lowest_common;
  3737.       int j, newlen = 0;
  3738.  
  3739.       /* Sort the items. */
  3740.       /* It is safe to sort this array, because the lowest common
  3741.          denominator found in matches[0] will remain in place. */
  3742.       for (i = 0; matches[i]; i++);
  3743.       qsort (matches, i, sizeof (char *), compare_strings);
  3744.  
  3745.       /* Remember the lowest common denominator for it may be unique. */
  3746.       lowest_common = savestring (matches[0]);
  3747.  
  3748.       for (i = 0; matches[i + 1]; i++)
  3749.         {
  3750.           if (strcmp (matches[i], matches[i + 1]) == 0)
  3751.         {
  3752.           free (matches[i]);
  3753.           matches[i] = (char *)-1;
  3754.         }
  3755.           else
  3756.         newlen++;
  3757.         }
  3758.  
  3759.       /* We have marked all the dead slots with (char *)-1.
  3760.          Copy all the non-dead entries into a new array. */
  3761.       {
  3762.         char **temp_array =
  3763.           (char **)malloc ((3 + newlen) * sizeof (char *));
  3764.  
  3765.         for (i = 1, j = 1; matches[i]; i++)
  3766.           {
  3767.         if (matches[i] != (char *)-1)
  3768.           temp_array[j++] = matches[i];
  3769.           }
  3770.  
  3771.         temp_array[j] = (char *)NULL;
  3772.  
  3773.         if (matches[0] != (char *)-1)
  3774.           free (matches[0]);
  3775.  
  3776.         free (matches);
  3777.  
  3778.         matches = temp_array;
  3779.       }
  3780.  
  3781.       /* Place the lowest common denominator back in [0]. */
  3782.       matches[0] = lowest_common;
  3783.  
  3784.       /* If there is one string left, and it is identical to the
  3785.          lowest common denominator, then the LCD is the string to
  3786.          insert. */
  3787.       if (j == 2 && strcmp (matches[0], matches[1]) == 0)
  3788.         {
  3789.           free (matches[1]);
  3790.           matches[1] = (char *)NULL;
  3791.         }
  3792.     }
  3793.  
  3794.       switch (what_to_do)
  3795.     {
  3796.     case TAB:
  3797.       /* If we are matching filenames, then here is our chance to
  3798.          do clever processing by re-examining the list.  Call the
  3799.          ignore function with the array as a parameter.  It can
  3800.          munge the array, deleting matches as it desires. */
  3801.       if (rl_ignore_some_completions_function &&
  3802.           our_func == (int (*)())filename_completion_function)
  3803.         (void)(*rl_ignore_some_completions_function)(matches);
  3804.  
  3805.       if (matches[0])
  3806.         {
  3807.           rl_delete_text (start, rl_point);
  3808.           rl_point = start;
  3809.           rl_insert_text (matches[0]);
  3810.         }
  3811.  
  3812.       /* If there are more matches, ring the bell to indicate.
  3813.          If this was the only match, and we are hacking files,
  3814.          check the file to see if it was a directory.  If so,
  3815.          add a '/' to the name.  If not, and we are at the end
  3816.          of the line, then add a space. */
  3817.       if (matches[1])
  3818.         {
  3819.           ding ();        /* There are other matches remaining. */
  3820.         }
  3821.       else
  3822.         {
  3823.           char temp_string[2];
  3824.  
  3825.           temp_string[0] = delimiter ? delimiter : ' ';
  3826.           temp_string[1] = '\0';
  3827.  
  3828.           if (rl_filename_completion_desired)
  3829.         {
  3830.           struct stat finfo;
  3831.           char *filename = tilde_expand (matches[0]);
  3832.  
  3833.           if ((stat (filename, &finfo) == 0) &&
  3834.               S_ISDIR (finfo.st_mode))
  3835.             {
  3836.               if (the_line[rl_point] != '/')
  3837.             rl_insert_text ("/");
  3838.             }
  3839.           else
  3840.             {
  3841.               if (rl_point == rl_end)
  3842.             rl_insert_text (temp_string);
  3843.             }
  3844.           free (filename);
  3845.         }
  3846.           else
  3847.         {
  3848.           if (rl_point == rl_end)
  3849.             rl_insert_text (temp_string);
  3850.         }
  3851.         }
  3852.       break;
  3853.  
  3854.     case '*':
  3855.       {
  3856.         int i = 1;
  3857.  
  3858.         rl_delete_text (start, rl_point);
  3859.         rl_point = start;
  3860.         rl_begin_undo_group ();
  3861.         if (matches[1])
  3862.           {
  3863.         while (matches[i])
  3864.           {
  3865.             rl_insert_text (matches[i++]);
  3866.             rl_insert_text (" ");
  3867.           }
  3868.           }
  3869.         else
  3870.           {
  3871.         rl_insert_text (matches[0]);
  3872.         rl_insert_text (" ");
  3873.           }
  3874.         rl_end_undo_group ();
  3875.       }
  3876.       break;
  3877.  
  3878.     case '?':
  3879.       {
  3880.         int len, count, limit, max = 0;
  3881.         int j, k, l;
  3882.  
  3883.         /* Handle simple case first.  What if there is only one answer? */
  3884.         if (!matches[1])
  3885.           {
  3886.         char *temp;
  3887.  
  3888.         if (rl_filename_completion_desired)
  3889.           temp = rindex (matches[0], '/');
  3890.         else
  3891.           temp = (char *)NULL;
  3892.  
  3893.         if (!temp)
  3894.           temp = matches[0];
  3895.         else
  3896.           temp++;
  3897.  
  3898.         crlf ();
  3899.         fprintf (out_stream, "%s", temp);
  3900.         crlf ();
  3901.         goto restart;
  3902.           }
  3903.  
  3904.         /* There is more than one answer.  Find out how many there are,
  3905.            and find out what the maximum printed length of a single entry
  3906.            is. */
  3907.         for (i = 1; matches[i]; i++)
  3908.           {
  3909.         char *temp = (char *)NULL;
  3910.  
  3911.         /* If we are hacking filenames, then only count the characters
  3912.            after the last slash in the pathname. */
  3913.         if (rl_filename_completion_desired)
  3914.           temp = rindex (matches[i], '/');
  3915.         else
  3916.           temp = (char *)NULL;
  3917.  
  3918.         if (!temp)
  3919.           temp = matches[i];
  3920.         else
  3921.           temp++;
  3922.  
  3923.         if (strlen (temp) > max)
  3924.           max = strlen (temp);
  3925.           }
  3926.  
  3927.         len = i;
  3928.  
  3929.         /* If there are many items, then ask the user if she
  3930.            really wants to see them all. */
  3931.         if (len >= rl_completion_query_items)
  3932.           {
  3933.         crlf ();
  3934.         fprintf (out_stream,
  3935.              "There are %d possibilities.  Do you really", len);
  3936.         crlf ();
  3937.         fprintf (out_stream, "wish to see them all? (y or n)");
  3938.         fflush (out_stream);
  3939.         if (!get_y_or_n ())
  3940.           {
  3941.             crlf ();
  3942.             goto restart;
  3943.           }
  3944.           }
  3945.         /* How many items of MAX length can we fit in the screen window? */
  3946.         max += 2;
  3947.         limit = screenwidth / max;
  3948.         if (limit != 1 && (limit * max == screenwidth))
  3949.           limit--;
  3950.  
  3951.         /* Avoid a possible floating exception.  If max > screenwidth,
  3952.            limit will be 0 and a divide-by-zero fault will result. */
  3953.         if (limit == 0)
  3954.           limit = 1;
  3955.  
  3956.         /* How many iterations of the printing loop? */
  3957.         count = (len + (limit - 1)) / limit;
  3958.  
  3959.         /* Watch out for special case.  If LEN is less than LIMIT, then
  3960.            just do the inner printing loop. */
  3961.         if (len < limit) count = 1;
  3962.  
  3963.         /* Sort the items if they are not already sorted. */
  3964.         if (!rl_ignore_completion_duplicates)
  3965.           qsort (matches, len, sizeof (char *), compare_strings);
  3966.  
  3967.         /* Print the sorted items, up-and-down alphabetically, like
  3968.            ls might. */
  3969.         crlf ();
  3970.  
  3971.         for (i = 1; i < count + 1; i++)
  3972.           {
  3973.         for (j = 0, l = i; j < limit; j++)
  3974.           {
  3975.             if (l > len || !matches[l])
  3976.               {
  3977.             break;
  3978.               }
  3979.             else
  3980.               {
  3981.             char *temp = (char *)NULL;
  3982.  
  3983.             if (rl_filename_completion_desired)
  3984.               temp = rindex (matches[l], '/');
  3985.             else
  3986.               temp = (char *)NULL;
  3987.  
  3988.             if (!temp)
  3989.               temp = matches[l];
  3990.             else
  3991.               temp++;
  3992.  
  3993.             fprintf (out_stream, "%s", temp);
  3994.             for (k = 0; k < max - strlen (temp); k++)
  3995.               putc (' ', out_stream);
  3996.               }
  3997.             l += count;
  3998.           }
  3999.         crlf ();
  4000.           }
  4001.       restart:
  4002.  
  4003.         rl_on_new_line ();
  4004.       }
  4005.       break;
  4006.  
  4007.     default:
  4008.       abort ();
  4009.     }
  4010.  
  4011.       for (i = 0; matches[i]; i++)
  4012.     free (matches[i]);
  4013.       free (matches);
  4014.     }
  4015.  
  4016.   /* Check to see if the line has changed through all of this manipulation. */
  4017.   if (saved_line_buffer)
  4018.     {
  4019.       if (strcmp (the_line, saved_line_buffer) != 0)
  4020.     completion_changed_buffer = 1;
  4021.       else
  4022.     completion_changed_buffer = 0;
  4023.  
  4024.       free (saved_line_buffer);
  4025.     }
  4026. }
  4027.  
  4028. /* Stupid comparison routine for qsort () ing strings. */
  4029. static int
  4030. compare_strings (s1, s2)
  4031.   char **s1, **s2;
  4032. {
  4033.   return (strcmp (*s1, *s2));
  4034. }
  4035.  
  4036. /* A completion function for usernames.
  4037.    TEXT contains a partial username preceded by a random
  4038.    character (usually `~').  */
  4039. char *
  4040. username_completion_function (text, state)
  4041.      int state;
  4042.      char *text;
  4043. {
  4044. #ifdef __GO32__
  4045.   return (char *)NULL;
  4046. #else /* !__GO32__ */
  4047.   static char *username = (char *)NULL;
  4048.   static struct passwd *entry;
  4049.   static int namelen, first_char, first_char_loc;
  4050.  
  4051.   if (!state)
  4052.     {
  4053.       if (username)
  4054.     free (username);
  4055.  
  4056.       first_char = *text;
  4057.  
  4058.       if (first_char == '~')
  4059.     first_char_loc = 1;
  4060.       else
  4061.     first_char_loc = 0;
  4062.  
  4063.       username = savestring (&text[first_char_loc]);
  4064.       namelen = strlen (username);
  4065.       setpwent ();
  4066.     }
  4067.  
  4068.   while (entry = getpwent ())
  4069.     {
  4070.       if (strncmp (username, entry->pw_name, namelen) == 0)
  4071.     break;
  4072.     }
  4073.  
  4074.   if (!entry)
  4075.     {
  4076.       endpwent ();
  4077.       return ((char *)NULL);
  4078.     }
  4079.   else
  4080.     {
  4081.       char *value = (char *)xmalloc (2 + strlen (entry->pw_name));
  4082.  
  4083.       *value = *text;
  4084.  
  4085.       strcpy (value + first_char_loc, entry->pw_name);
  4086.  
  4087.       if (first_char == '~')
  4088.     rl_filename_completion_desired = 1;
  4089.  
  4090.       return (value);
  4091.     }
  4092. #endif /* !__GO32__ */
  4093. }
  4094.  
  4095. /* **************************************************************** */
  4096. /*                                    */
  4097. /*            Undo, and Undoing                */
  4098. /*                                    */
  4099. /* **************************************************************** */
  4100.  
  4101. /* Non-zero tells rl_delete_text and rl_insert_text to not add to
  4102.    the undo list. */
  4103. int doing_an_undo = 0;
  4104.  
  4105. /* The current undo list for THE_LINE. */
  4106. UNDO_LIST *rl_undo_list = (UNDO_LIST *)NULL;
  4107.  
  4108. /* Remember how to undo something.  Concatenate some undos if that
  4109.    seems right. */
  4110. rl_add_undo (what, start, end, text)
  4111.      enum undo_code what;
  4112.      int start, end;
  4113.      char *text;
  4114. {
  4115.   UNDO_LIST *temp = (UNDO_LIST *)xmalloc (sizeof (UNDO_LIST));
  4116.   temp->what = what;
  4117.   temp->start = start;
  4118.   temp->end = end;
  4119.   temp->text = text;
  4120.   temp->next = rl_undo_list;
  4121.   rl_undo_list = temp;
  4122. }
  4123.  
  4124. /* Free the existing undo list. */
  4125. free_undo_list ()
  4126. {
  4127.   while (rl_undo_list) {
  4128.     UNDO_LIST *release = rl_undo_list;
  4129.     rl_undo_list = rl_undo_list->next;
  4130.  
  4131.     if (release->what == UNDO_DELETE)
  4132.       free (release->text);
  4133.  
  4134.     free (release);
  4135.   }
  4136. }
  4137.  
  4138. /* Undo the next thing in the list.  Return 0 if there
  4139.    is nothing to undo, or non-zero if there was. */
  4140. int
  4141. rl_do_undo ()
  4142. {
  4143.   UNDO_LIST *release;
  4144.   int waiting_for_begin = 0;
  4145.  
  4146. undo_thing:
  4147.   if (!rl_undo_list)
  4148.     return (0);
  4149.  
  4150.   doing_an_undo = 1;
  4151.  
  4152.   switch (rl_undo_list->what) {
  4153.  
  4154.     /* Undoing deletes means inserting some text. */
  4155.   case UNDO_DELETE:
  4156.     rl_point = rl_undo_list->start;
  4157.     rl_insert_text (rl_undo_list->text);
  4158.     free (rl_undo_list->text);
  4159.     break;
  4160.  
  4161.     /* Undoing inserts means deleting some text. */
  4162.   case UNDO_INSERT:
  4163.     rl_delete_text (rl_undo_list->start, rl_undo_list->end);
  4164.     rl_point = rl_undo_list->start;
  4165.     break;
  4166.  
  4167.     /* Undoing an END means undoing everything 'til we get to
  4168.        a BEGIN. */
  4169.   case UNDO_END:
  4170.     waiting_for_begin++;
  4171.     break;
  4172.  
  4173.     /* Undoing a BEGIN means that we are done with this group. */
  4174.   case UNDO_BEGIN:
  4175.     if (waiting_for_begin)
  4176.       waiting_for_begin--;
  4177.     else
  4178.       abort ();
  4179.     break;
  4180.   }
  4181.  
  4182.   doing_an_undo = 0;
  4183.  
  4184.   release = rl_undo_list;
  4185.   rl_undo_list = rl_undo_list->next;
  4186.   free (release);
  4187.  
  4188.   if (waiting_for_begin)
  4189.     goto undo_thing;
  4190.  
  4191.   return (1);
  4192. }
  4193.  
  4194. /* Begin a group.  Subsequent undos are undone as an atomic operation. */
  4195. rl_begin_undo_group ()
  4196. {
  4197.   rl_add_undo (UNDO_BEGIN, 0, 0, 0);
  4198. }
  4199.  
  4200. /* End an undo group started with rl_begin_undo_group (). */
  4201. rl_end_undo_group ()
  4202. {
  4203.   rl_add_undo (UNDO_END, 0, 0, 0);
  4204. }
  4205.  
  4206. /* Save an undo entry for the text from START to END. */
  4207. rl_modifying (start, end)
  4208.      int start, end;
  4209. {
  4210.   if (start > end)
  4211.     {
  4212.       int t = start;
  4213.       start = end;
  4214.       end = t;
  4215.     }
  4216.  
  4217.   if (start != end)
  4218.     {
  4219.       char *temp = rl_copy (start, end);
  4220.       rl_begin_undo_group ();
  4221.       rl_add_undo (UNDO_DELETE, start, end, temp);
  4222.       rl_add_undo (UNDO_INSERT, start, end, (char *)NULL);
  4223.       rl_end_undo_group ();
  4224.     }
  4225. }
  4226.  
  4227. /* Revert the current line to its previous state. */
  4228. rl_revert_line ()
  4229. {
  4230.   if (!rl_undo_list) ding ();
  4231.   else {
  4232.     while (rl_undo_list)
  4233.       rl_do_undo ();
  4234.   }
  4235. }
  4236.  
  4237. /* Do some undoing of things that were done. */
  4238. rl_undo_command (count)
  4239. {
  4240.   if (count < 0) return;    /* Nothing to do. */
  4241.  
  4242.   while (count)
  4243.     {
  4244.       if (rl_do_undo ())
  4245.     {
  4246.       count--;
  4247.     }
  4248.       else
  4249.     {
  4250.       ding ();
  4251.       break;
  4252.     }
  4253.     }
  4254. }
  4255.  
  4256. /* **************************************************************** */
  4257. /*                                    */
  4258. /*            History Utilities                */
  4259. /*                                    */
  4260. /* **************************************************************** */
  4261.  
  4262. /* We already have a history library, and that is what we use to control
  4263.    the history features of readline.  However, this is our local interface
  4264.    to the history mechanism. */
  4265.  
  4266. /* While we are editing the history, this is the saved
  4267.    version of the original line. */
  4268. HIST_ENTRY *saved_line_for_history = (HIST_ENTRY *)NULL;
  4269.  
  4270. /* Set the history pointer back to the last entry in the history. */
  4271. start_using_history ()
  4272. {
  4273.   using_history ();
  4274.   if (saved_line_for_history)
  4275.     free_history_entry (saved_line_for_history);
  4276.  
  4277.   saved_line_for_history = (HIST_ENTRY *)NULL;
  4278. }
  4279.  
  4280. /* Free the contents (and containing structure) of a HIST_ENTRY. */
  4281. free_history_entry (entry)
  4282.      HIST_ENTRY *entry;
  4283. {
  4284.   if (!entry) return;
  4285.   if (entry->line)
  4286.     free (entry->line);
  4287.   free (entry);
  4288. }
  4289.  
  4290. /* Perhaps put back the current line if it has changed. */
  4291. maybe_replace_line ()
  4292. {
  4293.   HIST_ENTRY *temp = current_history ();
  4294.  
  4295.   /* If the current line has changed, save the changes. */
  4296.   if (temp && ((UNDO_LIST *)(temp->data) != rl_undo_list))
  4297.     {
  4298.       temp = replace_history_entry (where_history (), the_line, rl_undo_list);
  4299.       free (temp->line);
  4300.       free (temp);
  4301.     }
  4302. }
  4303.  
  4304. /* Put back the saved_line_for_history if there is one. */
  4305. maybe_unsave_line ()
  4306. {
  4307.   if (saved_line_for_history)
  4308.     {
  4309.       int line_len;
  4310.  
  4311.       line_len = strlen (saved_line_for_history->line);
  4312.  
  4313.       if (line_len >= rl_line_buffer_len)
  4314.     rl_extend_line_buffer (line_len);
  4315.  
  4316.       strcpy (the_line, saved_line_for_history->line);
  4317.       rl_undo_list = (UNDO_LIST *)saved_line_for_history->data;
  4318.       free_history_entry (saved_line_for_history);
  4319.       saved_line_for_history = (HIST_ENTRY *)NULL;
  4320.       rl_end = rl_point = strlen (the_line);
  4321.     }
  4322.   else
  4323.     ding ();
  4324. }
  4325.  
  4326. /* Save the current line in saved_line_for_history. */
  4327. maybe_save_line ()
  4328. {
  4329.   if (!saved_line_for_history)
  4330.     {
  4331.       saved_line_for_history = (HIST_ENTRY *)xmalloc (sizeof (HIST_ENTRY));
  4332.       saved_line_for_history->line = savestring (the_line);
  4333.       saved_line_for_history->data = (char *)rl_undo_list;
  4334.     }
  4335. }
  4336.  
  4337. /* **************************************************************** */
  4338. /*                                    */
  4339. /*            History Commands                */
  4340. /*                                    */
  4341. /* **************************************************************** */
  4342.  
  4343. /* Meta-< goes to the start of the history. */
  4344. rl_beginning_of_history ()
  4345. {
  4346.   rl_get_previous_history (1 + where_history ());
  4347. }
  4348.  
  4349. /* Meta-> goes to the end of the history.  (The current line). */
  4350. rl_end_of_history ()
  4351. {
  4352.   maybe_replace_line ();
  4353.   using_history ();
  4354.   maybe_unsave_line ();
  4355. }
  4356.  
  4357. /* Move down to the next history line. */
  4358. rl_get_next_history (count)
  4359.      int count;
  4360. {
  4361.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4362.  
  4363.   if (count < 0)
  4364.     {
  4365.       rl_get_previous_history (-count);
  4366.       return;
  4367.     }
  4368.  
  4369.   if (!count)
  4370.     return;
  4371.  
  4372.   maybe_replace_line ();
  4373.  
  4374.   while (count)
  4375.     {
  4376.       temp = next_history ();
  4377.       if (!temp)
  4378.     break;
  4379.       --count;
  4380.     }
  4381.  
  4382.   if (!temp)
  4383.     maybe_unsave_line ();
  4384.   else
  4385.     {
  4386.       int line_len;
  4387.  
  4388.       line_len = strlen (temp->line);
  4389.  
  4390.       if (line_len >= rl_line_buffer_len)
  4391.     rl_extend_line_buffer (line_len);
  4392.  
  4393.       strcpy (the_line, temp->line);
  4394.       rl_undo_list = (UNDO_LIST *)temp->data;
  4395.       rl_end = rl_point = strlen (the_line);
  4396. #if defined (VI_MODE)
  4397.       if (rl_editing_mode == vi_mode)
  4398.     rl_point = 0;
  4399. #endif /* VI_MODE */
  4400.     }
  4401. }
  4402.  
  4403. /* Get the previous item out of our interactive history, making it the current
  4404.    line.  If there is no previous history, just ding. */
  4405. rl_get_previous_history (count)
  4406.      int count;
  4407. {
  4408.   HIST_ENTRY *old_temp = (HIST_ENTRY *)NULL;
  4409.   HIST_ENTRY *temp = (HIST_ENTRY *)NULL;
  4410.  
  4411.   if (count < 0)
  4412.     {
  4413.       rl_get_next_history (-count);
  4414.       return;
  4415.     }
  4416.  
  4417.   if (!count)
  4418.     return;
  4419.  
  4420.   /* If we don't have a line saved, then save this one. */
  4421.   maybe_save_line ();
  4422.  
  4423.   /* If the current line has changed, save the changes. */
  4424.   maybe_replace_line ();
  4425.  
  4426.   while (count)
  4427.     {
  4428.       temp = previous_history ();
  4429.       if (!temp)
  4430.     break;
  4431.       else
  4432.     old_temp = temp;
  4433.       --count;
  4434.     }
  4435.  
  4436.   /* If there was a large argument, and we moved back to the start of the
  4437.      history, that is not an error.  So use the last value found. */
  4438.   if (!temp && old_temp)
  4439.     temp = old_temp;
  4440.  
  4441.   if (!temp)
  4442.     ding ();
  4443.   else
  4444.     {
  4445.       int line_len;
  4446.  
  4447.       line_len = strlen (temp->line);
  4448.  
  4449.       if (line_len >= rl_line_buffer_len)
  4450.     rl_extend_line_buffer (line_len);
  4451.  
  4452.       strcpy (the_line, temp->line);
  4453.       rl_undo_list = (UNDO_LIST *)temp->data;
  4454.       rl_end = rl_point = line_len;
  4455.  
  4456. #if defined (VI_MODE)
  4457.       if (rl_editing_mode == vi_mode)
  4458.     rl_point = 0;
  4459. #endif /* VI_MODE */
  4460.     }
  4461. }
  4462.  
  4463.  
  4464. /* **************************************************************** */
  4465. /*                                    */
  4466. /*            I-Search and Searching                */
  4467. /*                                    */
  4468. /* **************************************************************** */
  4469.  
  4470. /* Search backwards through the history looking for a string which is typed
  4471.    interactively.  Start with the current line. */
  4472. rl_reverse_search_history (sign, key)
  4473.      int sign;
  4474.      int key;
  4475. {
  4476.   rl_search_history (-sign, key);
  4477. }
  4478.  
  4479. /* Search forwards through the history looking for a string which is typed
  4480.    interactively.  Start with the current line. */
  4481. rl_forward_search_history (sign, key)
  4482.      int sign;
  4483.      int key;
  4484. {
  4485.   rl_search_history (sign, key);
  4486. }
  4487.  
  4488. /* Display the current state of the search in the echo-area.
  4489.    SEARCH_STRING contains the string that is being searched for,
  4490.    DIRECTION is zero for forward, or 1 for reverse,
  4491.    WHERE is the history list number of the current line.  If it is
  4492.    -1, then this line is the starting one. */
  4493. rl_display_search (search_string, reverse_p, where)
  4494.      char *search_string;
  4495.      int reverse_p, where;
  4496. {
  4497.   char *message = (char *)NULL;
  4498.  
  4499.   message =
  4500.     (char *)alloca (1 + (search_string ? strlen (search_string) : 0) + 30);
  4501.  
  4502.   *message = '\0';
  4503.  
  4504. #if defined (NOTDEF)
  4505.   if (where != -1)
  4506.     sprintf (message, "[%d]", where + history_base);
  4507. #endif /* NOTDEF */
  4508.  
  4509.   strcat (message, "(");
  4510.  
  4511.   if (reverse_p)
  4512.     strcat (message, "reverse-");
  4513.  
  4514.   strcat (message, "i-search)`");
  4515.  
  4516.   if (search_string)
  4517.     strcat (message, search_string);
  4518.  
  4519.   strcat (message, "': ");
  4520.   rl_message (message, 0, 0);
  4521.   rl_redisplay ();
  4522. }
  4523.  
  4524. /* Search through the history looking for an interactively typed string.
  4525.    This is analogous to i-search.  We start the search in the current line.
  4526.    DIRECTION is which direction to search; >= 0 means forward, < 0 means
  4527.    backwards. */
  4528. rl_search_history (direction, invoking_key)
  4529.      int direction;
  4530.      int invoking_key;
  4531. {
  4532.   /* The string that the user types in to search for. */
  4533.   char *search_string = (char *)alloca (128);
  4534.  
  4535.   /* The current length of SEARCH_STRING. */
  4536.   int search_string_index;
  4537.  
  4538.   /* The list of lines to search through. */
  4539.   char **lines;
  4540.  
  4541.   /* The length of LINES. */
  4542.   int hlen;
  4543.  
  4544.   /* Where we get LINES from. */
  4545.   HIST_ENTRY **hlist = history_list ();
  4546.  
  4547.   register int i = 0;
  4548.   int orig_point = rl_point;
  4549.   int orig_line = where_history ();
  4550.   int last_found_line = orig_line;
  4551.   int c, done = 0;
  4552.  
  4553.   /* The line currently being searched. */
  4554.   char *sline;
  4555.  
  4556.   /* Offset in that line. */
  4557.   int index;
  4558.  
  4559.   /* Non-zero if we are doing a reverse search. */
  4560.   int reverse = (direction < 0);
  4561.  
  4562.   /* Create an arrary of pointers to the lines that we want to search. */
  4563.   maybe_replace_line ();
  4564.   if (hlist)
  4565.     for (i = 0; hlist[i]; i++);
  4566.  
  4567.   /* Allocate space for this many lines, +1 for the current input line,
  4568.      and remember those lines. */
  4569.   lines = (char **)alloca ((1 + (hlen = i)) * sizeof (char *));
  4570.   for (i = 0; i < hlen; i++)
  4571.     lines[i] = hlist[i]->line;
  4572.  
  4573.   if (saved_line_for_history)
  4574.     lines[i] = saved_line_for_history->line;
  4575.   else
  4576.     /* So I have to type it in this way instead. */
  4577.     {
  4578.       char *alloced_line;
  4579.  
  4580.       /* Keep that mips alloca happy. */
  4581.       alloced_line = (char *)alloca (1 + strlen (the_line));
  4582.       lines[i] = alloced_line;
  4583.       strcpy (lines[i], &the_line[0]);
  4584.     }
  4585.  
  4586.   hlen++;
  4587.  
  4588.   /* The line where we start the search. */
  4589.   i = orig_line;
  4590.  
  4591.   /* Initialize search parameters. */
  4592.   *search_string = '\0';
  4593.   search_string_index = 0;
  4594.  
  4595.   /* Normalize DIRECTION into 1 or -1. */
  4596.   if (direction >= 0)
  4597.     direction = 1;
  4598.   else
  4599.     direction = -1;
  4600.  
  4601.   rl_display_search (search_string, reverse, -1);
  4602.  
  4603.   sline = the_line;
  4604.   index = rl_point;
  4605.  
  4606.   while (!done)
  4607.     {
  4608.       c = rl_read_key ();
  4609.  
  4610.       /* Hack C to Do What I Mean. */
  4611.       {
  4612.     Function *f = (Function *)NULL;
  4613.  
  4614.     if (keymap[c].type == ISFUNC)
  4615.       {
  4616.         f = keymap[c].function;
  4617.  
  4618.         if (f == rl_reverse_search_history)
  4619.           c = reverse ? -1 : -2;
  4620.         else if (f == rl_forward_search_history)
  4621.           c =  !reverse ? -1 : -2;
  4622.       }
  4623.       }
  4624.  
  4625.       switch (c)
  4626.     {
  4627.     case ESC:
  4628.       done = 1;
  4629.       continue;
  4630.  
  4631.       /* case invoking_key: */
  4632.     case -1:
  4633.       goto search_again;
  4634.  
  4635.       /* switch directions */
  4636.     case -2:
  4637.       direction = -direction;
  4638.       reverse = (direction < 0);
  4639.  
  4640.       goto do_search;
  4641.  
  4642.     case CTRL ('G'):
  4643.       strcpy (the_line, lines[orig_line]);
  4644.       rl_point = orig_point;
  4645.       rl_end = strlen (the_line);
  4646.       rl_clear_message ();
  4647.       return;
  4648.  
  4649.     default:
  4650.       if (c < 32 || c > 126)
  4651.         {
  4652.           rl_execute_next (c);
  4653.           done = 1;
  4654.           continue;
  4655.         }
  4656.       else
  4657.         {
  4658.           search_string[search_string_index++] = c;
  4659.           search_string[search_string_index] = '\0';
  4660.           goto do_search;
  4661.  
  4662.         search_again:
  4663.  
  4664.           if (!search_string_index)
  4665.         continue;
  4666.           else
  4667.         {
  4668.           if (reverse)
  4669.             --index;
  4670.           else
  4671.             if (index != strlen (sline))
  4672.               ++index;
  4673.             else
  4674.               ding ();
  4675.         }
  4676.         do_search:
  4677.  
  4678.           while (1)
  4679.         {
  4680.           if (reverse)
  4681.             {
  4682.               while (index >= 0)
  4683.             if (strncmp
  4684.                 (search_string, sline + index, search_string_index)
  4685.                 == 0)
  4686.               goto string_found;
  4687.             else
  4688.               index--;
  4689.             }
  4690.           else
  4691.             {
  4692.               register int limit =
  4693.             (strlen (sline) - search_string_index) + 1;
  4694.  
  4695.               while (index < limit)
  4696.             {
  4697.               if (strncmp (search_string,
  4698.                        sline + index,
  4699.                        search_string_index) == 0)
  4700.                 goto string_found;
  4701.               index++;
  4702.             }
  4703.             }
  4704.  
  4705.         next_line:
  4706.           i += direction;
  4707.  
  4708.           /* At limit for direction? */
  4709.           if ((reverse && i < 0) ||
  4710.               (!reverse && i == hlen))
  4711.             goto search_failed;
  4712.  
  4713.           sline = lines[i];
  4714.           if (reverse)
  4715.             index = strlen (sline);
  4716.           else
  4717.             index = 0;
  4718.  
  4719.           /* If the search string is longer than the current
  4720.              line, no match. */
  4721.           if (search_string_index > strlen (sline))
  4722.             goto next_line;
  4723.  
  4724.           /* Start actually searching. */
  4725.           if (reverse)
  4726.             index -= search_string_index;
  4727.         }
  4728.  
  4729.         search_failed:
  4730.           /* We cannot find the search string.  Ding the bell. */
  4731.           ding ();
  4732.           i = last_found_line;
  4733.           break;
  4734.  
  4735.         string_found:
  4736.           /* We have found the search string.  Just display it.  But don't
  4737.          actually move there in the history list until the user accepts
  4738.          the location. */
  4739.           {
  4740.         int line_len;
  4741.  
  4742.         line_len = strlen (lines[i]);
  4743.  
  4744.         if (line_len >= rl_line_buffer_len)
  4745.           rl_extend_line_buffer (line_len);
  4746.  
  4747.         strcpy (the_line, lines[i]);
  4748.         rl_point = index;
  4749.         rl_end = line_len;
  4750.         last_found_line = i;
  4751.         rl_display_search
  4752.           (search_string, reverse, (i == orig_line) ? -1 : i);
  4753.           }
  4754.         }
  4755.     }
  4756.       continue;
  4757.     }
  4758.  
  4759.   /* The searching is over.  The user may have found the string that she
  4760.      was looking for, or else she may have exited a failing search.  If
  4761.      INDEX is -1, then that shows that the string searched for was not
  4762.      found.  We use this to determine where to place rl_point. */
  4763.   {
  4764.     int now = last_found_line;
  4765.  
  4766.     /* First put back the original state. */
  4767.     strcpy (the_line, lines[orig_line]);
  4768.  
  4769.     if (now < orig_line)
  4770.       rl_get_previous_history (orig_line - now);
  4771.     else
  4772.       rl_get_next_history (now - orig_line);
  4773.  
  4774.     /* If the index of the "matched" string is less than zero, then the
  4775.        final search string was never matched, so put point somewhere
  4776.        reasonable. */
  4777.     if (index < 0)
  4778.       index = strlen (the_line);
  4779.  
  4780.     rl_point = index;
  4781.     rl_clear_message ();
  4782.   }
  4783. }
  4784.  
  4785. /* Make C be the next command to be executed. */
  4786. rl_execute_next (c)
  4787.      int c;
  4788. {
  4789.   rl_pending_input = c;
  4790. }
  4791.  
  4792. /* **************************************************************** */
  4793. /*                                    */
  4794. /*            Killing Mechanism                */
  4795. /*                                    */
  4796. /* **************************************************************** */
  4797.  
  4798. /* What we assume for a max number of kills. */
  4799. #define DEFAULT_MAX_KILLS 10
  4800.  
  4801. /* The real variable to look at to find out when to flush kills. */
  4802. int rl_max_kills = DEFAULT_MAX_KILLS;
  4803.  
  4804. /* Where to store killed text. */
  4805. char **rl_kill_ring = (char **)NULL;
  4806.  
  4807. /* Where we are in the kill ring. */
  4808. int rl_kill_index = 0;
  4809.  
  4810. /* How many slots we have in the kill ring. */
  4811. int rl_kill_ring_length = 0;
  4812.  
  4813. /* How to say that you only want to save a certain amount
  4814.    of kill material. */
  4815. rl_set_retained_kills (num)
  4816.      int num;
  4817. {}
  4818.  
  4819. /* The way to kill something.  This appends or prepends to the last
  4820.    kill, if the last command was a kill command.  if FROM is less
  4821.    than TO, then the text is appended, otherwise prepended.  If the
  4822.    last command was not a kill command, then a new slot is made for
  4823.    this kill. */
  4824. rl_kill_text (from, to)
  4825.      int from, to;
  4826. {
  4827.   int slot;
  4828.   char *text = rl_copy (from, to);
  4829.  
  4830.   /* Is there anything to kill? */
  4831.   if (from == to)
  4832.     {
  4833.       free (text);
  4834.       last_command_was_kill++;
  4835.       return;
  4836.     }
  4837.  
  4838.   /* Delete the copied text from the line. */
  4839.   rl_delete_text (from, to);
  4840.  
  4841.   /* First, find the slot to work with. */
  4842.   if (!last_command_was_kill)
  4843.     {
  4844.       /* Get a new slot.  */
  4845.       if (!rl_kill_ring)
  4846.     {
  4847.       /* If we don't have any defined, then make one. */
  4848.       rl_kill_ring = (char **)
  4849.         xmalloc (((rl_kill_ring_length = 1) + 1) * sizeof (char *));
  4850.       slot = 1;
  4851.     }
  4852.       else
  4853.     {
  4854.       /* We have to add a new slot on the end, unless we have
  4855.          exceeded the max limit for remembering kills. */
  4856.       slot = rl_kill_ring_length;
  4857.       if (slot == rl_max_kills)
  4858.         {
  4859.           register int i;
  4860.           free (rl_kill_ring[0]);
  4861.           for (i = 0; i < slot; i++)
  4862.         rl_kill_ring[i] = rl_kill_ring[i + 1];
  4863.         }
  4864.       else
  4865.         {
  4866.           rl_kill_ring =
  4867.         (char **)
  4868.           xrealloc (rl_kill_ring,
  4869.                 ((slot = (rl_kill_ring_length += 1)) + 1)
  4870.                 * sizeof (char *));
  4871.         }
  4872.     }
  4873.       slot--;
  4874.     }
  4875.   else
  4876.     {
  4877.       slot = rl_kill_ring_length - 1;
  4878.     }
  4879.  
  4880.   /* If the last command was a kill, prepend or append. */
  4881.   if (last_command_was_kill && rl_editing_mode != vi_mode)
  4882.     {
  4883.       char *old = rl_kill_ring[slot];
  4884.       char *new = (char *)xmalloc (1 + strlen (old) + strlen (text));
  4885.  
  4886.       if (from < to)
  4887.     {
  4888.       strcpy (new, old);
  4889.       strcat (new, text);
  4890.     }
  4891.       else
  4892.     {
  4893.       strcpy (new, text);
  4894.       strcat (new, old);
  4895.     }
  4896.       free (old);
  4897.       free (text);
  4898.       rl_kill_ring[slot] = new;
  4899.     }
  4900.   else
  4901.     {
  4902.       rl_kill_ring[slot] = text;
  4903.     }
  4904.   rl_kill_index = slot;
  4905.   last_command_was_kill++;
  4906. }
  4907.  
  4908. /* Now REMEMBER!  In order to do prepending or appending correctly, kill
  4909.    commands always make rl_point's original position be the FROM argument,
  4910.    and rl_point's extent be the TO argument. */
  4911.  
  4912. /* **************************************************************** */
  4913. /*                                    */
  4914. /*            Killing Commands                */
  4915. /*                                    */
  4916. /* **************************************************************** */
  4917.  
  4918. /* Delete the word at point, saving the text in the kill ring. */
  4919. rl_kill_word (count)
  4920.      int count;
  4921. {
  4922.   int orig_point = rl_point;
  4923.  
  4924.   if (count < 0)
  4925.     rl_backward_kill_word (-count);
  4926.   else
  4927.     {
  4928.       rl_forward_word (count);
  4929.  
  4930.       if (rl_point != orig_point)
  4931.     rl_kill_text (orig_point, rl_point);
  4932.  
  4933.       rl_point = orig_point;
  4934.     }
  4935. }
  4936.  
  4937. /* Rubout the word before point, placing it on the kill ring. */
  4938. rl_backward_kill_word (count)
  4939.      int count;
  4940. {
  4941.   int orig_point = rl_point;
  4942.  
  4943.   if (count < 0)
  4944.     rl_kill_word (-count);
  4945.   else
  4946.     {
  4947.       rl_backward_word (count);
  4948.  
  4949.       if (rl_point != orig_point)
  4950.     rl_kill_text (orig_point, rl_point);
  4951.     }
  4952. }
  4953.  
  4954. /* Kill from here to the end of the line.  If DIRECTION is negative, kill
  4955.    back to the line start instead. */
  4956. rl_kill_line (direction)
  4957.      int direction;
  4958. {
  4959.   int orig_point = rl_point;
  4960.  
  4961.   if (direction < 0)
  4962.     rl_backward_kill_line (1);
  4963.   else
  4964.     {
  4965.       rl_end_of_line ();
  4966.       if (orig_point != rl_point)
  4967.     rl_kill_text (orig_point, rl_point);
  4968.       rl_point = orig_point;
  4969.     }
  4970. }
  4971.  
  4972. /* Kill backwards to the start of the line.  If DIRECTION is negative, kill
  4973.    forwards to the line end instead. */
  4974. rl_backward_kill_line (direction)
  4975.      int direction;
  4976. {
  4977.   int orig_point = rl_point;
  4978.  
  4979.   if (direction < 0)
  4980.     rl_kill_line (1);
  4981.   else
  4982.     {
  4983.       if (!rl_point)
  4984.     ding ();
  4985.       else
  4986.     {
  4987.       rl_beg_of_line ();
  4988.       rl_kill_text (orig_point, rl_point);
  4989.     }
  4990.     }
  4991. }
  4992.  
  4993. /* Yank back the last killed text.  This ignores arguments. */
  4994. rl_yank ()
  4995. {
  4996.   if (!rl_kill_ring) rl_abort ();
  4997.   rl_insert_text (rl_kill_ring[rl_kill_index]);
  4998. }
  4999.  
  5000. /* If the last command was yank, or yank_pop, and the text just
  5001.    before point is identical to the current kill item, then
  5002.    delete that text from the line, rotate the index down, and
  5003.    yank back some other text. */
  5004. rl_yank_pop ()
  5005. {
  5006.   int l;
  5007.  
  5008.   if (((rl_last_func != rl_yank_pop) && (rl_last_func != rl_yank)) ||
  5009.       !rl_kill_ring)
  5010.     {
  5011.       rl_abort ();
  5012.     }
  5013.  
  5014.   l = strlen (rl_kill_ring[rl_kill_index]);
  5015.   if (((rl_point - l) >= 0) &&
  5016.       (strncmp (the_line + (rl_point - l),
  5017.         rl_kill_ring[rl_kill_index], l) == 0))
  5018.     {
  5019.       rl_delete_text ((rl_point - l), rl_point);
  5020.       rl_point -= l;
  5021.       rl_kill_index--;
  5022.       if (rl_kill_index < 0)
  5023.     rl_kill_index = rl_kill_ring_length - 1;
  5024.       rl_yank ();
  5025.     }
  5026.   else
  5027.     rl_abort ();
  5028.  
  5029. }
  5030.  
  5031. /* Yank the COUNTth argument from the previous history line. */
  5032. rl_yank_nth_arg (count, ignore)
  5033.      int count;
  5034. {
  5035.   register HIST_ENTRY *entry = previous_history ();
  5036.   char *arg;
  5037.  
  5038.   if (entry)
  5039.     next_history ();
  5040.   else
  5041.     {
  5042.       ding ();
  5043.       return;
  5044.     }
  5045.  
  5046.   arg = history_arg_extract (count, count, entry->line);
  5047.   if (!arg || !*arg)
  5048.     {
  5049.       ding ();
  5050.       return;
  5051.     }
  5052.  
  5053.   rl_begin_undo_group ();
  5054.  
  5055. #if defined (VI_MODE)
  5056.   /* Vi mode always inserts a space befoe yanking the argument, and it
  5057.      inserts it right *after* rl_point. */
  5058.   if (rl_editing_mode == vi_mode)
  5059.     rl_point++;
  5060. #endif /* VI_MODE */
  5061.  
  5062.   if (rl_point && the_line[rl_point - 1] != ' ')
  5063.     rl_insert_text (" ");
  5064.  
  5065.   rl_insert_text (arg);
  5066.   free (arg);
  5067.  
  5068.   rl_end_undo_group ();
  5069. }
  5070.  
  5071. /* How to toggle back and forth between editing modes. */
  5072. rl_vi_editing_mode ()
  5073. {
  5074. #if defined (VI_MODE)
  5075.   rl_editing_mode = vi_mode;
  5076.   rl_vi_insertion_mode ();
  5077. #endif /* VI_MODE */
  5078. }
  5079.  
  5080. rl_emacs_editing_mode ()
  5081. {
  5082.   rl_editing_mode = emacs_mode;
  5083.   keymap = emacs_standard_keymap;
  5084. }
  5085.  
  5086.  
  5087. /* **************************************************************** */
  5088. /*                                    */
  5089. /*                 Completion                    */
  5090. /*                                    */
  5091. /* **************************************************************** */
  5092.  
  5093. /* Non-zero means that case is not significant in completion. */
  5094. int completion_case_fold = 0;
  5095.  
  5096. /* Return an array of (char *) which is a list of completions for TEXT.
  5097.    If there are no completions, return a NULL pointer.
  5098.    The first entry in the returned array is the substitution for TEXT.
  5099.    The remaining entries are the possible completions.
  5100.    The array is terminated with a NULL pointer.
  5101.  
  5102.    ENTRY_FUNCTION is a function of two args, and returns a (char *).
  5103.      The first argument is TEXT.
  5104.      The second is a state argument; it should be zero on the first call, and
  5105.      non-zero on subsequent calls.  It returns a NULL pointer to the caller
  5106.      when there are no more matches.
  5107.  */
  5108. char **
  5109. completion_matches (text, entry_function)
  5110.      char *text;
  5111.      char *(*entry_function) ();
  5112. {
  5113.   /* Number of slots in match_list. */
  5114.   int match_list_size;
  5115.  
  5116.   /* The list of matches. */
  5117.   char **match_list =
  5118.     (char **)xmalloc (((match_list_size = 10) + 1) * sizeof (char *));
  5119.  
  5120.   /* Number of matches actually found. */
  5121.   int matches = 0;
  5122.  
  5123.   /* Temporary string binder. */
  5124.   char *string;
  5125.  
  5126.   match_list[1] = (char *)NULL;
  5127.  
  5128.   while (string = (*entry_function) (text, matches))
  5129.     {
  5130.       if (matches + 1 == match_list_size)
  5131.     match_list = (char **)xrealloc
  5132.       (match_list, ((match_list_size += 10) + 1) * sizeof (char *));
  5133.  
  5134.       match_list[++matches] = string;
  5135.       match_list[matches + 1] = (char *)NULL;
  5136.     }
  5137.  
  5138.   /* If there were any matches, then look through them finding out the
  5139.      lowest common denominator.  That then becomes match_list[0]. */
  5140.   if (matches)
  5141.     {
  5142.       register int i = 1;
  5143.       int low = 100000;        /* Count of max-matched characters. */
  5144.  
  5145.       /* If only one match, just use that. */
  5146.       if (matches == 1)
  5147.     {
  5148.       match_list[0] = match_list[1];
  5149.       match_list[1] = (char *)NULL;
  5150.     }
  5151.       else
  5152.     {
  5153.       /* Otherwise, compare each member of the list with
  5154.          the next, finding out where they stop matching. */
  5155.  
  5156.       while (i < matches)
  5157.         {
  5158.           register int c1, c2, si;
  5159.  
  5160.           if (completion_case_fold)
  5161.         {
  5162.           for (si = 0;
  5163.                (c1 = to_lower(match_list[i][si])) &&
  5164.                (c2 = to_lower(match_list[i + 1][si]));
  5165.                si++)
  5166.             if (c1 != c2) break;
  5167.         }
  5168.           else
  5169.         {
  5170.           for (si = 0;
  5171.                (c1 = match_list[i][si]) &&
  5172.                (c2 = match_list[i + 1][si]);
  5173.                si++)
  5174.             if (c1 != c2) break;
  5175.         }
  5176.  
  5177.           if (low > si) low = si;
  5178.           i++;
  5179.         }
  5180.       match_list[0] = (char *)xmalloc (low + 1);
  5181.       strncpy (match_list[0], match_list[1], low);
  5182.       match_list[0][low] = '\0';
  5183.     }
  5184.     }
  5185.   else                /* There were no matches. */
  5186.     {
  5187.       free (match_list);
  5188.       match_list = (char **)NULL;
  5189.     }
  5190.   return (match_list);
  5191. }
  5192.  
  5193. /* Okay, now we write the entry_function for filename completion.  In the
  5194.    general case.  Note that completion in the shell is a little different
  5195.    because of all the pathnames that must be followed when looking up the
  5196.    completion for a command. */
  5197. char *
  5198. filename_completion_function (text, state)
  5199.      int state;
  5200.      char *text;
  5201. {
  5202.   static DIR *directory;
  5203.   static char *filename = (char *)NULL;
  5204.   static char *dirname = (char *)NULL;
  5205.   static char *users_dirname = (char *)NULL;
  5206.   static int filename_len;
  5207.  
  5208.   dirent *entry = (dirent *)NULL;
  5209.  
  5210.   /* If we don't have any state, then do some initialization. */
  5211.   if (!state)
  5212.     {
  5213.       char *temp;
  5214.  
  5215.       if (dirname) free (dirname);
  5216.       if (filename) free (filename);
  5217.       if (users_dirname) free (users_dirname);
  5218.  
  5219.       filename = savestring (text);
  5220.       if (!*text) text = ".";
  5221.       dirname = savestring (text);
  5222.  
  5223.       temp = rindex (dirname, '/');
  5224.  
  5225.       if (temp)
  5226.     {
  5227.       strcpy (filename, ++temp);
  5228.       *temp = '\0';
  5229.     }
  5230.       else
  5231.     strcpy (dirname, ".");
  5232.  
  5233.       /* We aren't done yet.  We also support the "~user" syntax. */
  5234.  
  5235.       /* Save the version of the directory that the user typed. */
  5236.       users_dirname = savestring (dirname);
  5237.       {
  5238.     char *temp_dirname;
  5239.  
  5240.     temp_dirname = tilde_expand (dirname);
  5241.     free (dirname);
  5242.     dirname = temp_dirname;
  5243.  
  5244.     if (rl_symbolic_link_hook)
  5245.       (*rl_symbolic_link_hook) (&dirname);
  5246.       }
  5247.       directory = opendir (dirname);
  5248.       filename_len = strlen (filename);
  5249.  
  5250.       rl_filename_completion_desired = 1;
  5251.     }
  5252.  
  5253.   /* At this point we should entertain the possibility of hacking wildcarded
  5254.      filenames, like /usr/man/man<WILD>/te<TAB>.  If the directory name
  5255.      contains globbing characters, then build an array of directories to
  5256.      glob on, and glob on the first one. */
  5257.  
  5258.   /* Now that we have some state, we can read the directory. */
  5259.  
  5260.   while (directory && (entry = readdir (directory)))
  5261.     {
  5262.       /* Special case for no filename.
  5263.      All entries except "." and ".." match. */
  5264.       if (!filename_len)
  5265.     {
  5266.       if ((strcmp (entry->d_name, ".") != 0) &&
  5267.           (strcmp (entry->d_name, "..") != 0))
  5268.         break;
  5269.     }
  5270.       else
  5271.     {
  5272.       /* Otherwise, if these match upto the length of filename, then
  5273.          it is a match. */
  5274.         if (entry->d_name[0] == filename[0] && /* Quick test */
  5275.         (strncmp (filename, entry->d_name, filename_len) == 0))
  5276.           {
  5277.         break;
  5278.           }
  5279.     }
  5280.     }
  5281.  
  5282.   if (!entry)
  5283.     {
  5284.       if (directory)
  5285.     {
  5286.       closedir (directory);
  5287.       directory = (DIR *)NULL;
  5288.     }
  5289.       return (char *)NULL;
  5290.     }
  5291.   else
  5292.     {
  5293.       char *temp;
  5294.  
  5295.       if (dirname && (strcmp (dirname, ".") != 0))
  5296.     {
  5297.       temp = (char *)
  5298.         xmalloc (1 + strlen (users_dirname) + strlen (entry->d_name));
  5299.       strcpy (temp, users_dirname);
  5300.       strcat (temp, entry->d_name);
  5301.     }
  5302.       else
  5303.     {
  5304.       temp = (savestring (entry->d_name));
  5305.     }
  5306.       return (temp);
  5307.     }
  5308. }
  5309.  
  5310.  
  5311. /* **************************************************************** */
  5312. /*                                    */
  5313. /*            Binding keys                    */
  5314. /*                                    */
  5315. /* **************************************************************** */
  5316.  
  5317. /* rl_add_defun (char *name, Function *function, int key)
  5318.    Add NAME to the list of named functions.  Make FUNCTION
  5319.    be the function that gets called.
  5320.    If KEY is not -1, then bind it. */
  5321. rl_add_defun (name, function, key)
  5322.      char *name;
  5323.      Function *function;
  5324.      int key;
  5325. {
  5326.   if (key != -1)
  5327.     rl_bind_key (key, function);
  5328.   rl_add_funmap_entry (name, function);
  5329. }
  5330.  
  5331. /* Bind KEY to FUNCTION.  Returns non-zero if KEY is out of range. */
  5332. int
  5333. rl_bind_key (key, function)
  5334.      int key;
  5335.      Function *function;
  5336. {
  5337.   if (key < 0)
  5338.     return (key);
  5339.  
  5340.   if (key > 127 && key < 256)
  5341.     {
  5342.       if (keymap[ESC].type == ISKMAP)
  5343.     {
  5344.       Keymap escmap = (Keymap)keymap[ESC].function;
  5345.  
  5346.       key -= 128;
  5347.       escmap[key].type = ISFUNC;
  5348.       escmap[key].function = function;
  5349.       return (0);
  5350.     }
  5351.       return (key);
  5352.     }
  5353.  
  5354.   keymap[key].type = ISFUNC;
  5355.   keymap[key].function = function;
  5356.  return (0);
  5357. }
  5358.  
  5359. /* Bind KEY to FUNCTION in MAP.  Returns non-zero in case of invalid
  5360.    KEY. */
  5361. int
  5362. rl_bind_key_in_map (key, function, map)
  5363.      int key;
  5364.      Function *function;
  5365.      Keymap map;
  5366. {
  5367.   int result;
  5368.   Keymap oldmap = keymap;
  5369.  
  5370.   keymap = map;
  5371.   result = rl_bind_key (key, function);
  5372.   keymap = oldmap;
  5373.   return (result);
  5374. }
  5375.  
  5376. /* Make KEY do nothing in the currently selected keymap.
  5377.    Returns non-zero in case of error. */
  5378. int
  5379. rl_unbind_key (key)
  5380.      int key;
  5381. {
  5382.   return (rl_bind_key (key, (Function *)NULL));
  5383. }
  5384.  
  5385. /* Make KEY do nothing in MAP.
  5386.    Returns non-zero in case of error. */
  5387. int
  5388. rl_unbind_key_in_map (key, map)
  5389.      int key;
  5390.      Keymap map;
  5391. {
  5392.   return (rl_bind_key_in_map (key, (Function *)NULL, map));
  5393. }
  5394.  
  5395. /* Bind the key sequence represented by the string KEYSEQ to
  5396.    FUNCTION.  This makes new keymaps as necessary.  The initial
  5397.    place to do bindings is in MAP. */
  5398. rl_set_key (keyseq, function, map)
  5399.      char *keyseq;
  5400.      Function *function;
  5401.      Keymap map;
  5402. {
  5403.   rl_generic_bind (ISFUNC, keyseq, function, map);
  5404. }
  5405.  
  5406. /* Bind the key sequence represented by the string KEYSEQ to
  5407.    the string of characters MACRO.  This makes new keymaps as
  5408.    necessary.  The initial place to do bindings is in MAP. */
  5409. rl_macro_bind (keyseq, macro, map)
  5410.      char *keyseq, *macro;
  5411.      Keymap map;
  5412. {
  5413.   char *macro_keys;
  5414.   int macro_keys_len;
  5415.  
  5416.   macro_keys = (char *)xmalloc ((2 * strlen (macro)) + 1);
  5417.  
  5418.   if (rl_translate_keyseq (macro, macro_keys, ¯o_keys_len))
  5419.     {
  5420.       free (macro_keys);
  5421.       return;
  5422.     }
  5423.   rl_generic_bind (ISMACR, keyseq, macro_keys, map);
  5424. }
  5425.  
  5426. /* Bind the key sequence represented by the string KEYSEQ to
  5427.    the arbitrary pointer DATA.  TYPE says what kind of data is
  5428.    pointed to by DATA, right now this can be a function (ISFUNC),
  5429.    a macro (ISMACR), or a keymap (ISKMAP).  This makes new keymaps
  5430.    as necessary.  The initial place to do bindings is in MAP. */
  5431. rl_generic_bind (type, keyseq, data, map)
  5432.      int type;
  5433.      char *keyseq, *data;
  5434.      Keymap map;
  5435. {
  5436.   char *keys;
  5437.   int keys_len;
  5438.   register int i;
  5439.  
  5440.   /* If no keys to bind to, exit right away. */
  5441.   if (!keyseq || !*keyseq)
  5442.     {
  5443.       if (type == ISMACR)
  5444.     free (data);
  5445.       return;
  5446.     }
  5447.  
  5448.   keys = (char *)alloca (1 + (2 * strlen (keyseq)));
  5449.  
  5450.   /* Translate the ASCII representation of KEYSEQ into an array
  5451.      of characters.  Stuff the characters into ARRAY, and the
  5452.      length of ARRAY into LENGTH. */
  5453.   if (rl_translate_keyseq (keyseq, keys, &keys_len))
  5454.     return;
  5455.  
  5456.   /* Bind keys, making new keymaps as necessary. */
  5457.   for (i = 0; i < keys_len; i++)
  5458.     {
  5459.       if (i + 1 < keys_len)
  5460.     {
  5461.       if (map[keys[i]].type != ISKMAP)
  5462.         {
  5463.           if (map[i].type == ISMACR)
  5464.         free ((char *)map[i].function);
  5465.  
  5466.           map[keys[i]].type = ISKMAP;
  5467.           map[keys[i]].function = (Function *)rl_make_bare_keymap ();
  5468.         }
  5469.       map = (Keymap)map[keys[i]].function;
  5470.     }
  5471.       else
  5472.     {
  5473.       if (map[keys[i]].type == ISMACR)
  5474.         free ((char *)map[keys[i]].function);
  5475.  
  5476.       map[keys[i]].function = (Function *)data;
  5477.       map[keys[i]].type = type;
  5478.     }
  5479.     }
  5480. }
  5481.  
  5482. /* Translate the ASCII representation of SEQ, stuffing the
  5483.    values into ARRAY, an array of characters.  LEN gets the
  5484.    final length of ARRAY.  Return non-zero if there was an
  5485.    error parsing SEQ. */
  5486. rl_translate_keyseq (seq, array, len)
  5487.      char *seq, *array;
  5488.      int *len;
  5489. {
  5490.   register int i, c, l = 0;
  5491.  
  5492.   for (i = 0; c = seq[i]; i++)
  5493.     {
  5494.       if (c == '\\')
  5495.     {
  5496.       c = seq[++i];
  5497.  
  5498.       if (!c)
  5499.         break;
  5500.  
  5501.       if (((c == 'C' || c == 'M') &&  seq[i + 1] == '-') ||
  5502.           (c == 'e'))
  5503.         {
  5504.           /* Handle special case of backwards define. */
  5505.           if (strncmp (&seq[i], "C-\\M-", 5) == 0)
  5506.         {
  5507.           array[l++] = ESC;
  5508.           i += 5;
  5509.           array[l++] = CTRL (to_upper (seq[i]));
  5510.           if (!seq[i])
  5511.             i--;
  5512.           continue;
  5513.         }
  5514.  
  5515.           switch (c)
  5516.         {
  5517.         case 'M':
  5518.           i++;
  5519.           array[l++] = ESC;
  5520.           break;
  5521.  
  5522.         case 'C':
  5523.           i += 2;
  5524.           /* Special hack for C-?... */
  5525.           if (seq[i] == '?')
  5526.             array[l++] = RUBOUT;
  5527.           else
  5528.             array[l++] = CTRL (to_upper (seq[i]));
  5529.           break;
  5530.  
  5531.         case 'e':
  5532.           array[l++] = ESC;
  5533.         }
  5534.  
  5535.           continue;
  5536.         }
  5537.     }
  5538.       array[l++] = c;
  5539.     }
  5540.  
  5541.   *len = l;
  5542.   array[l] = '\0';
  5543.   return (0);
  5544. }
  5545.  
  5546. /* Return a pointer to the function that STRING represents.
  5547.    If STRING doesn't have a matching function, then a NULL pointer
  5548.    is returned. */
  5549. Function *
  5550. rl_named_function (string)
  5551.      char *string;
  5552. {
  5553.   register int i;
  5554.  
  5555.   for (i = 0; funmap[i]; i++)
  5556.     if (stricmp (funmap[i]->name, string) == 0)
  5557.       return (funmap[i]->function);
  5558.   return ((Function *)NULL);
  5559. }
  5560.  
  5561. /* The last key bindings file read. */
  5562. #ifdef __MSDOS__
  5563. /* Don't know what to do, but this is a guess */
  5564. static char *last_readline_init_file = "/INPUTRC";
  5565. #else
  5566. static char *last_readline_init_file = "~/inputrc";
  5567. #endif
  5568.  
  5569. /* Re-read the current keybindings file. */
  5570. rl_re_read_init_file (count, ignore)
  5571.      int count, ignore;
  5572. {
  5573.   rl_read_init_file ((char *)NULL);
  5574. }
  5575.  
  5576. /* Do key bindings from a file.  If FILENAME is NULL it defaults
  5577.    to `~/.inputrc'.  If the file existed and could be opened and
  5578.    read, 0 is returned, otherwise errno is returned. */
  5579. int
  5580. rl_read_init_file (filename)
  5581.      char *filename;
  5582. {
  5583.   register int i;
  5584.   char *buffer, *openname, *line, *end;
  5585.   struct stat finfo;
  5586.   int file;
  5587.  
  5588.   /* Default the filename. */
  5589.   if (!filename)
  5590.     filename = last_readline_init_file;
  5591.  
  5592.   openname = tilde_expand (filename);
  5593.  
  5594.   if (!openname || *openname == '\000')
  5595.     return ENOENT;
  5596.  
  5597.   if ((stat (openname, &finfo) < 0) ||
  5598.       (file = open (openname, O_RDONLY, 0666)) < 0)
  5599.     {
  5600.       free (openname);
  5601.       return (errno);
  5602.     }
  5603.   else
  5604.     free (openname);
  5605.  
  5606.   last_readline_init_file = filename;
  5607.  
  5608.   /* Read the file into BUFFER. */
  5609.   buffer = (char *)xmalloc (finfo.st_size + 1);
  5610.   i = read (file, buffer, finfo.st_size);
  5611.   close (file);
  5612.  
  5613.   if (i != finfo.st_size)
  5614.     return (errno);
  5615.  
  5616.   /* Loop over the lines in the file.  Lines that start with `#' are
  5617.      comments; all other lines are commands for readline initialization. */
  5618.   line = buffer;
  5619.   end = buffer + finfo.st_size;
  5620.   while (line < end)
  5621.     {
  5622.       /* Find the end of this line. */
  5623.       for (i = 0; line + i != end && line[i] != '\n'; i++);
  5624.  
  5625.       /* Mark end of line. */
  5626.       line[i] = '\0';
  5627.  
  5628.       /* If the line is not a comment, then parse it. */
  5629.       if (*line != '#')
  5630.     rl_parse_and_bind (line);
  5631.  
  5632.       /* Move to the next line. */
  5633.       line += i + 1;
  5634.     }
  5635.   return (0);
  5636. }
  5637.  
  5638. /* **************************************************************** */
  5639. /*                                    */
  5640. /*            Parser Directives                   */
  5641. /*                                    */
  5642. /* **************************************************************** */
  5643.  
  5644. /* Conditionals. */
  5645.  
  5646. /* Calling programs set this to have their argv[0]. */
  5647. char *rl_readline_name = "other";
  5648.  
  5649. /* Stack of previous values of parsing_conditionalized_out. */
  5650. static unsigned char *if_stack = (unsigned char *)NULL;
  5651. static int if_stack_depth = 0;
  5652. static int if_stack_size = 0;
  5653.  
  5654. /* Push parsing_conditionalized_out, and set parser state based on ARGS. */
  5655. parser_if (args)
  5656.      char *args;
  5657. {
  5658.   register int i;
  5659.  
  5660.   /* Push parser state. */
  5661.   if (if_stack_depth + 1 >= if_stack_size)
  5662.     {
  5663.       if (!if_stack)
  5664.     if_stack = (unsigned char *)xmalloc (if_stack_size = 20);
  5665.       else
  5666.     if_stack = (unsigned char *)xrealloc (if_stack, if_stack_size += 20);
  5667.     }
  5668.   if_stack[if_stack_depth++] = parsing_conditionalized_out;
  5669.  
  5670.   /* If parsing is turned off, then nothing can turn it back on except
  5671.      for finding the matching endif.  In that case, return right now. */
  5672.   if (parsing_conditionalized_out)
  5673.     return;
  5674.  
  5675.   /* Isolate first argument. */
  5676.   for (i = 0; args[i] && !whitespace (args[i]); i++);
  5677.  
  5678.   if (args[i])
  5679.     args[i++] = '\0';
  5680.  
  5681.   /* Handle "if term=foo" and "if mode=emacs" constructs.  If this
  5682.      isn't term=foo, or mode=emacs, then check to see if the first
  5683.      word in ARGS is the same as the value stored in rl_readline_name. */
  5684.   if (rl_terminal_name && strnicmp (args, "term=", 5) == 0)
  5685.     {
  5686.       char *tem, *tname;
  5687.  
  5688.       /* Terminals like "aaa-60" are equivalent to "aaa". */
  5689.       tname = savestring (rl_terminal_name);
  5690.       tem = rindex (tname, '-');
  5691.       if (tem)
  5692.     *tem = '\0';
  5693.  
  5694.       if (stricmp (args + 5, tname) == 0)
  5695.     parsing_conditionalized_out = 0;
  5696.       else
  5697.     parsing_conditionalized_out = 1;
  5698.     }
  5699. #if defined (VI_MODE)
  5700.   else if (strnicmp (args, "mode=", 5) == 0)
  5701.     {
  5702.       int mode;
  5703.  
  5704.       if (stricmp (args + 5, "emacs") == 0)
  5705.     mode = emacs_mode;
  5706.       else if (stricmp (args + 5, "vi") == 0)
  5707.     mode = vi_mode;
  5708.       else
  5709.     mode = no_mode;
  5710.  
  5711.       if (mode == rl_editing_mode)
  5712.     parsing_conditionalized_out = 0;
  5713.       else
  5714.     parsing_conditionalized_out = 1;
  5715.     }
  5716. #endif /* VI_MODE */
  5717.   /* Check to see if the first word in ARGS is the same as the
  5718.      value stored in rl_readline_name. */
  5719.   else if (stricmp (args, rl_readline_name) == 0)
  5720.     parsing_conditionalized_out = 0;
  5721.   else
  5722.     parsing_conditionalized_out = 1;
  5723. }
  5724.  
  5725. /* Invert the current parser state if there is anything on the stack. */
  5726. parser_else (args)
  5727.      char *args;
  5728. {
  5729.   register int i;
  5730.  
  5731.   if (!if_stack_depth)
  5732.     {
  5733.       /* Error message? */
  5734.       return;
  5735.     }
  5736.  
  5737.   /* Check the previous (n - 1) levels of the stack to make sure that
  5738.      we haven't previously turned off parsing. */
  5739.   for (i = 0; i < if_stack_depth - 1; i++)
  5740.     if (if_stack[i] == 1)
  5741.       return;
  5742.  
  5743.   /* Invert the state of parsing if at top level. */
  5744.   parsing_conditionalized_out = !parsing_conditionalized_out;
  5745. }
  5746.  
  5747. /* Terminate a conditional, popping the value of
  5748.    parsing_conditionalized_out from the stack. */
  5749. parser_endif (args)
  5750.      char *args;
  5751. {
  5752.   if (if_stack_depth)
  5753.     parsing_conditionalized_out = if_stack[--if_stack_depth];
  5754.   else
  5755.     {
  5756.       /* *** What, no error message? *** */
  5757.     }
  5758. }
  5759.  
  5760. /* Associate textual names with actual functions. */
  5761. static struct {
  5762.   char *name;
  5763.   Function *function;
  5764. } parser_directives [] = {
  5765.   { "if", parser_if },
  5766.   { "endif", parser_endif },
  5767.   { "else", parser_else },
  5768.   { (char *)0x0, (Function *)0x0 }
  5769. };
  5770.  
  5771. /* Handle a parser directive.  STATEMENT is the line of the directive
  5772.    without any leading `$'. */
  5773. static int
  5774. handle_parser_directive (statement)
  5775.      char *statement;
  5776. {
  5777.   register int i;
  5778.   char *directive, *args;
  5779.  
  5780.   /* Isolate the actual directive. */
  5781.  
  5782.   /* Skip whitespace. */
  5783.   for (i = 0; whitespace (statement[i]); i++);
  5784.  
  5785.   directive = &statement[i];
  5786.  
  5787.   for (; statement[i] && !whitespace (statement[i]); i++);
  5788.  
  5789.   if (statement[i])
  5790.     statement[i++] = '\0';
  5791.  
  5792.   for (; statement[i] && whitespace (statement[i]); i++);
  5793.  
  5794.   args = &statement[i];
  5795.  
  5796.   /* Lookup the command, and act on it. */
  5797.   for (i = 0; parser_directives[i].name; i++)
  5798.     if (stricmp (directive, parser_directives[i].name) == 0)
  5799.       {
  5800.     (*parser_directives[i].function) (args);
  5801.     return (0);
  5802.       }
  5803.  
  5804.   /* *** Should an error message be output? */
  5805.   return (1);
  5806. }
  5807.  
  5808. /* Ugly but working hack for binding prefix meta. */
  5809. #define PREFIX_META_HACK
  5810.  
  5811. static int substring_member_of_array ();
  5812.  
  5813. /* Read the binding command from STRING and perform it.
  5814.    A key binding command looks like: Keyname: function-name\0,
  5815.    a variable binding command looks like: set variable value.
  5816.    A new-style keybinding looks like "\C-x\C-x": exchange-point-and-mark. */
  5817. rl_parse_and_bind (string)
  5818.      char *string;
  5819. {
  5820.   extern char *possible_control_prefixes[], *possible_meta_prefixes[];
  5821.   char *funname, *kname;
  5822.   register int c;
  5823.   int key, i;
  5824.  
  5825.   while (string && whitespace (*string))
  5826.     string++;
  5827.  
  5828.   if (!string || !*string || *string == '#')
  5829.     return;
  5830.  
  5831.   /* If this is a parser directive, act on it. */
  5832.   if (*string == '$')
  5833.     {
  5834.       handle_parser_directive (&string[1]);
  5835.       return;
  5836.     }
  5837.  
  5838.   /* If we are supposed to be skipping parsing right now, then do it. */
  5839.   if (parsing_conditionalized_out)
  5840.     return;
  5841.  
  5842.   i = 0;
  5843.   /* If this keyname is a complex key expression surrounded by quotes,
  5844.      advance to after the matching close quote. */
  5845.   if (*string == '"')
  5846.     {
  5847.       for (i = 1; c = string[i]; i++)
  5848.     {
  5849.       if (c == '"' && string[i - 1] != '\\')
  5850.         break;
  5851.     }
  5852.     }
  5853.  
  5854.   /* Advance to the colon (:) or whitespace which separates the two objects. */
  5855.   for (; (c = string[i]) && c != ':' && c != ' ' && c != '\t'; i++ );
  5856.  
  5857.   /* Mark the end of the command (or keyname). */
  5858.   if (string[i])
  5859.     string[i++] = '\0';
  5860.  
  5861.   /* If this is a command to set a variable, then do that. */
  5862.   if (stricmp (string, "set") == 0)
  5863.     {
  5864.       char *var = string + i;
  5865.       char *value;
  5866.  
  5867.       /* Make VAR point to start of variable name. */
  5868.       while (*var && whitespace (*var)) var++;
  5869.  
  5870.       /* Make value point to start of value string. */
  5871.       value = var;
  5872.       while (*value && !whitespace (*value)) value++;
  5873.       if (*value)
  5874.     *value++ = '\0';
  5875.       while (*value && whitespace (*value)) value++;
  5876.  
  5877.       rl_variable_bind (var, value);
  5878.       return;
  5879.     }
  5880.  
  5881.   /* Skip any whitespace between keyname and funname. */
  5882.   for (; string[i] && whitespace (string[i]); i++);
  5883.   funname = &string[i];
  5884.  
  5885.   /* Now isolate funname.
  5886.      For straight function names just look for whitespace, since
  5887.      that will signify the end of the string.  But this could be a
  5888.      macro definition.  In that case, the string is quoted, so skip
  5889.      to the matching delimiter. */
  5890.   if (*funname == '\'' || *funname == '"')
  5891.     {
  5892.       int delimiter = string[i++];
  5893.  
  5894.       for (; c = string[i]; i++)
  5895.     {
  5896.       if (c == delimiter && string[i - 1] != '\\')
  5897.         break;
  5898.     }
  5899.       if (c)
  5900.     i++;
  5901.     }
  5902.  
  5903.   /* Advance to the end of the string.  */
  5904.   for (; string[i] && !whitespace (string[i]); i++);
  5905.  
  5906.   /* No extra whitespace at the end of the string. */
  5907.   string[i] = '\0';
  5908.  
  5909.   /* If this is a new-style key-binding, then do the binding with
  5910.      rl_set_key ().  Otherwise, let the older code deal with it. */
  5911.   if (*string == '"')
  5912.     {
  5913.       char *seq = (char *)alloca (1 + strlen (string));
  5914.       register int j, k = 0;
  5915.  
  5916.       for (j = 1; string[j]; j++)
  5917.     {
  5918.       if (string[j] == '"' && string[j - 1] != '\\')
  5919.         break;
  5920.  
  5921.       seq[k++] = string[j];
  5922.     }
  5923.       seq[k] = '\0';
  5924.  
  5925.       /* Binding macro? */
  5926.       if (*funname == '\'' || *funname == '"')
  5927.     {
  5928.       j = strlen (funname);
  5929.  
  5930.       if (j && funname[j - 1] == *funname)
  5931.         funname[j - 1] = '\0';
  5932.  
  5933.       rl_macro_bind (seq, &funname[1], keymap);
  5934.     }
  5935.       else
  5936.     rl_set_key (seq, rl_named_function (funname), keymap);
  5937.  
  5938.       return;
  5939.     }
  5940.  
  5941.   /* Get the actual character we want to deal with. */
  5942.   kname = rindex (string, '-');
  5943.   if (!kname)
  5944.     kname = string;
  5945.   else
  5946.     kname++;
  5947.  
  5948.   key = glean_key_from_name (kname);
  5949.  
  5950.   /* Add in control and meta bits. */
  5951.   if (substring_member_of_array (string, possible_control_prefixes))
  5952.     key = CTRL (to_upper (key));
  5953.  
  5954.   if (substring_member_of_array (string, possible_meta_prefixes))
  5955.     key = META (key);
  5956.  
  5957.   /* Temporary.  Handle old-style keyname with macro-binding. */
  5958.   if (*funname == '\'' || *funname == '"')
  5959.     {
  5960.       char seq[2];
  5961.       int fl = strlen (funname);
  5962.  
  5963.       seq[0] = key; seq[1] = '\0';
  5964.       if (fl && funname[fl - 1] == *funname)
  5965.     funname[fl - 1] = '\0';
  5966.  
  5967.       rl_macro_bind (seq, &funname[1], keymap);
  5968.     }
  5969. #if defined (PREFIX_META_HACK)
  5970.   /* Ugly, but working hack to keep prefix-meta around. */
  5971.   else if (stricmp (funname, "prefix-meta") == 0)
  5972.     {
  5973.       char seq[2];
  5974.  
  5975.       seq[0] = key;
  5976.       seq[1] = '\0';
  5977.       rl_generic_bind (ISKMAP, seq, (char *)emacs_meta_keymap, keymap);
  5978.     }
  5979. #endif /* PREFIX_META_HACK */
  5980.   else
  5981.     rl_bind_key (key, rl_named_function (funname));
  5982. }
  5983.  
  5984. rl_variable_bind (name, value)
  5985.      char *name, *value;
  5986. {
  5987.   if (stricmp (name, "editing-mode") == 0)
  5988.     {
  5989.       if (strnicmp (value, "vi", 2) == 0)
  5990.     {
  5991. #if defined (VI_MODE)
  5992.       keymap = vi_insertion_keymap;
  5993.       rl_editing_mode = vi_mode;
  5994. #else
  5995. #if defined (NOTDEF)
  5996.       /* What state is the terminal in?  I'll tell you:
  5997.          non-determinate!  That means we cannot do any output. */
  5998.       ding ();
  5999. #endif /* NOTDEF */
  6000. #endif /* VI_MODE */
  6001.     }
  6002.       else if (strnicmp (value, "emacs", 5) == 0)
  6003.     {
  6004.       keymap = emacs_standard_keymap;
  6005.       rl_editing_mode = emacs_mode;
  6006.     }
  6007.     }
  6008.   else if (stricmp (name, "horizontal-scroll-mode") == 0)
  6009.     {
  6010.       if (!*value || stricmp (value, "On") == 0)
  6011.     horizontal_scroll_mode = 1;
  6012.       else
  6013.     horizontal_scroll_mode = 0;
  6014.     }
  6015.   else if (stricmp (name, "mark-modified-lines") == 0)
  6016.     {
  6017.       if (!*value || stricmp (value, "On") == 0)
  6018.     mark_modified_lines = 1;
  6019.       else
  6020.     mark_modified_lines = 0;
  6021.     }
  6022.   else if (stricmp (name, "prefer-visible-bell") == 0)
  6023.     {
  6024.       if (!*value || stricmp (value, "On") == 0)
  6025.         prefer_visible_bell = 1;
  6026.       else
  6027.         prefer_visible_bell = 0;
  6028.     }
  6029.   else if (stricmp (name, "comment-begin") == 0)
  6030.     {
  6031. #if defined (VI_MODE)
  6032.       extern char *rl_vi_comment_begin;
  6033.  
  6034.       if (*value)
  6035.     {
  6036.       if (rl_vi_comment_begin)
  6037.         free (rl_vi_comment_begin);
  6038.  
  6039.       rl_vi_comment_begin = savestring (value);
  6040.     }
  6041. #endif /* VI_MODE */
  6042.     }
  6043. }
  6044.  
  6045. /* Return the character which matches NAME.
  6046.    For example, `Space' returns ' '. */
  6047.  
  6048. typedef struct {
  6049.   char *name;
  6050.   int value;
  6051. } assoc_list;
  6052.  
  6053. assoc_list name_key_alist[] = {
  6054.   { "DEL", 0x7f },
  6055.   { "ESC", '\033' },
  6056.   { "Escape", '\033' },
  6057.   { "LFD", '\n' },
  6058.   { "Newline", '\n' },
  6059.   { "RET", '\r' },
  6060.   { "Return", '\r' },
  6061.   { "Rubout", 0x7f },
  6062.   { "SPC", ' ' },
  6063.   { "Space", ' ' },
  6064.   { "Tab", 0x09 },
  6065.   { (char *)0x0, 0 }
  6066. };
  6067.  
  6068. int
  6069. glean_key_from_name (name)
  6070.      char *name;
  6071. {
  6072.   register int i;
  6073.  
  6074.   for (i = 0; name_key_alist[i].name; i++)
  6075.     if (stricmp (name, name_key_alist[i].name) == 0)
  6076.       return (name_key_alist[i].value);
  6077.  
  6078.   return (*name);
  6079. }
  6080.  
  6081.  
  6082. /* **************************************************************** */
  6083. /*                                    */
  6084. /*          Key Binding and Function Information            */
  6085. /*                                    */
  6086. /* **************************************************************** */
  6087.  
  6088. /* Each of the following functions produces information about the
  6089.    state of keybindings and functions known to Readline.  The info
  6090.    is always printed to rl_outstream, and in such a way that it can
  6091.    be read back in (i.e., passed to rl_parse_and_bind (). */
  6092.  
  6093. /* Print the names of functions known to Readline. */
  6094. void
  6095. rl_list_funmap_names (ignore)
  6096.      int ignore;
  6097. {
  6098.   register int i;
  6099.   char **funmap_names;
  6100.   extern char **rl_funmap_names ();
  6101.  
  6102.   funmap_names = rl_funmap_names ();
  6103.  
  6104.   if (!funmap_names)
  6105.     return;
  6106.  
  6107.   for (i = 0; funmap_names[i]; i++)
  6108.     fprintf (rl_outstream, "%s\n", funmap_names[i]);
  6109.  
  6110.   free (funmap_names);
  6111. }
  6112.  
  6113. /* Return a NULL terminated array of strings which represent the key
  6114.    sequences that are used to invoke FUNCTION in MAP. */
  6115. static char **
  6116. invoking_keyseqs_in_map (function, map)
  6117.      Function *function;
  6118.      Keymap map;
  6119. {
  6120.   register int key;
  6121.   char **result;
  6122.   int result_index, result_size;
  6123.  
  6124.   result = (char **)NULL;
  6125.   result_index = result_size = 0;
  6126.  
  6127.   for (key = 0; key < 128; key++)
  6128.     {
  6129.       switch (map[key].type)
  6130.     {
  6131.     case ISMACR:
  6132.       /* Macros match, if, and only if, the pointers are identical.
  6133.          Thus, they are treated exactly like functions in here. */
  6134.     case ISFUNC:
  6135.       /* If the function in the keymap is the one we are looking for,
  6136.          then add the current KEY to the list of invoking keys. */
  6137.       if (map[key].function == function)
  6138.         {
  6139.           char *keyname = (char *)xmalloc (5);
  6140.  
  6141.           if (CTRL_P (key))
  6142.         sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6143.           else if (key == RUBOUT)
  6144.         sprintf (keyname, "\\C-?");
  6145.           else
  6146.         sprintf (keyname, "%c", key);
  6147.           
  6148.           if (result_index + 2 > result_size)
  6149.         {
  6150.           if (!result)
  6151.             result = (char **) xmalloc
  6152.               ((result_size = 10) * sizeof (char *));
  6153.           else
  6154.             result = (char **) xrealloc
  6155.               (result, (result_size += 10) * sizeof (char *));
  6156.         }
  6157.  
  6158.           result[result_index++] = keyname;
  6159.           result[result_index] = (char *)NULL;
  6160.         }
  6161.       break;
  6162.  
  6163.     case ISKMAP:
  6164.       {
  6165.         char **seqs = (char **)NULL;
  6166.  
  6167.         /* Find the list of keyseqs in this map which have FUNCTION as
  6168.            their target.  Add the key sequences found to RESULT. */
  6169.         if (map[key].function)
  6170.           seqs =
  6171.         invoking_keyseqs_in_map (function, (Keymap)map[key].function);
  6172.  
  6173.         if (seqs)
  6174.           {
  6175.         register int i;
  6176.  
  6177.         for (i = 0; seqs[i]; i++)
  6178.           {
  6179.             char *keyname = (char *)xmalloc (6 + strlen (seqs[i]));
  6180.  
  6181.             if (key == ESC)
  6182.               sprintf (keyname, "\\e");
  6183.             else if (CTRL_P (key))
  6184.               sprintf (keyname, "\\C-%c", to_lower (UNCTRL (key)));
  6185.             else if (key == RUBOUT)
  6186.               sprintf (keyname, "\\C-?");
  6187.             else
  6188.               sprintf (keyname, "%c", key);
  6189.  
  6190.             strcat (keyname, seqs[i]);
  6191.  
  6192.             if (result_index + 2 > result_size)
  6193.               {
  6194.             if (!result)
  6195.               result = (char **)
  6196.                 xmalloc ((result_size = 10) * sizeof (char *));
  6197.             else
  6198.               result = (char **)
  6199.                 xrealloc (result,
  6200.                       (result_size += 10) * sizeof (char *));
  6201.               }
  6202.  
  6203.             result[result_index++] = keyname;
  6204.             result[result_index] = (char *)NULL;
  6205.           }
  6206.           }
  6207.       }
  6208.       break;
  6209.     }
  6210.     }
  6211.   return (result);
  6212. }
  6213.  
  6214. /* Return a NULL terminated array of strings which represent the key
  6215.    sequences that can be used to invoke FUNCTION using the current keymap. */
  6216. char **
  6217. rl_invoking_keyseqs (function)
  6218.      Function *function;
  6219. {
  6220.   return (invoking_keyseqs_in_map (function, keymap));
  6221. }
  6222.  
  6223. /* Print all of the current functions and their bindings to
  6224.    rl_outstream.  If an explicit argument is given, then print
  6225.    the output in such a way that it can be read back in. */
  6226. int
  6227. rl_dump_functions (count)
  6228.      int count;
  6229. {
  6230.   void rl_function_dumper ();
  6231.  
  6232.   rl_function_dumper (rl_explicit_arg);
  6233.   rl_on_new_line ();
  6234.   return (0);
  6235. }
  6236.  
  6237. /* Print all of the functions and their bindings to rl_outstream.  If
  6238.    PRINT_READABLY is non-zero, then print the output in such a way
  6239.    that it can be read back in. */
  6240. void
  6241. rl_function_dumper (print_readably)
  6242.      int print_readably;
  6243. {
  6244.   register int i;
  6245.   char **rl_funmap_names (), **names;
  6246.   char *name;
  6247.  
  6248.   names = rl_funmap_names ();
  6249.  
  6250.   fprintf (rl_outstream, "\n");
  6251.  
  6252.   for (i = 0; name = names[i]; i++)
  6253.     {
  6254.       Function *function;
  6255.       char **invokers;
  6256.  
  6257.       function = rl_named_function (name);
  6258.       invokers = invoking_keyseqs_in_map (function, keymap);
  6259.  
  6260.       if (print_readably)
  6261.     {
  6262.       if (!invokers)
  6263.         fprintf (rl_outstream, "# %s (not bound)\n", name);
  6264.       else
  6265.         {
  6266.           register int j;
  6267.  
  6268.           for (j = 0; invokers[j]; j++)
  6269.         {
  6270.           fprintf (rl_outstream, "\"%s\": %s\n",
  6271.                invokers[j], name);
  6272.           free (invokers[j]);
  6273.         }
  6274.  
  6275.           free (invokers);
  6276.         }
  6277.     }
  6278.       else
  6279.     {
  6280.       if (!invokers)
  6281.         fprintf (rl_outstream, "%s is not bound to any keys\n",
  6282.              name);
  6283.       else
  6284.         {
  6285.           register int j;
  6286.  
  6287.           fprintf (rl_outstream, "%s can be found on ", name);
  6288.  
  6289.           for (j = 0; invokers[j] && j < 5; j++)
  6290.         {
  6291.           fprintf (rl_outstream, "\"%s\"%s", invokers[j],
  6292.                invokers[j + 1] ? ", " : ".\n");
  6293.         }
  6294.  
  6295.           if (j == 5 && invokers[j])
  6296.         fprintf (rl_outstream, "...\n");
  6297.  
  6298.           for (j = 0; invokers[j]; j++)
  6299.         free (invokers[j]);
  6300.  
  6301.           free (invokers);
  6302.         }
  6303.     }
  6304.     }
  6305. }
  6306.  
  6307.  
  6308. /* **************************************************************** */
  6309. /*                                    */
  6310. /*            String Utility Functions            */
  6311. /*                                    */
  6312. /* **************************************************************** */
  6313.  
  6314. static char *strindex ();
  6315.  
  6316. /* Return non-zero if any members of ARRAY are a substring in STRING. */
  6317. static int
  6318. substring_member_of_array (string, array)
  6319.      char *string, **array;
  6320. {
  6321.   while (*array)
  6322.     {
  6323.       if (strindex (string, *array))
  6324.     return (1);
  6325.       array++;
  6326.     }
  6327.   return (0);
  6328. }
  6329.  
  6330. /* Whoops, Unix doesn't have strnicmp. */
  6331.  
  6332. /* Compare at most COUNT characters from string1 to string2.  Case
  6333.    doesn't matter. */
  6334. static int
  6335. strnicmp (string1, string2, count)
  6336.      char *string1, *string2;
  6337. {
  6338.   register char ch1, ch2;
  6339.  
  6340.   while (count)
  6341.     {
  6342.       ch1 = *string1++;
  6343.       ch2 = *string2++;
  6344.       if (to_upper(ch1) == to_upper(ch2))
  6345.     count--;
  6346.       else break;
  6347.     }
  6348.   return (count);
  6349. }
  6350.  
  6351. /* strcmp (), but caseless. */
  6352. static int
  6353. stricmp (string1, string2)
  6354.      char *string1, *string2;
  6355. {
  6356.   register char ch1, ch2;
  6357.  
  6358.   while (*string1 && *string2)
  6359.     {
  6360.       ch1 = *string1++;
  6361.       ch2 = *string2++;
  6362.       if (to_upper(ch1) != to_upper(ch2))
  6363.     return (1);
  6364.     }
  6365.   return (*string1 | *string2);
  6366. }
  6367.  
  6368. /* Determine if s2 occurs in s1.  If so, return a pointer to the
  6369.    match in s1.  The compare is case insensitive. */
  6370. static char *
  6371. strindex (s1, s2)
  6372.      register char *s1, *s2;
  6373. {
  6374.   register int i, l = strlen (s2);
  6375.   register int len = strlen (s1);
  6376.  
  6377.   for (i = 0; (len - i) >= l; i++)
  6378.     if (strnicmp (&s1[i], s2, l) == 0)
  6379.       return (s1 + i);
  6380.   return ((char *)NULL);
  6381. }
  6382.  
  6383.  
  6384. /* **************************************************************** */
  6385. /*                                    */
  6386. /*            USG (System V) Support                */
  6387. /*                                    */
  6388. /* **************************************************************** */
  6389.  
  6390. /* When compiling and running in the `Posix' environment, Ultrix does
  6391.    not restart system calls, so this needs to do it. */
  6392. int
  6393. rl_getc (stream)
  6394.      FILE *stream;
  6395. {
  6396.   int result;
  6397.   unsigned char c;
  6398.  
  6399. #ifdef __GO32__
  6400.   if (isatty(0))
  6401.     return getkey();
  6402. #endif /* __GO32__ */
  6403.  
  6404.   while (1)
  6405.     {
  6406.       result = read (fileno (stream), &c, sizeof (char));
  6407.  
  6408.       if (result == sizeof (char))
  6409.     return (c);
  6410.  
  6411.       /* If zero characters are returned, then the file that we are
  6412.      reading from is empty!  Return EOF in that case. */
  6413.       if (result == 0)
  6414.     return (EOF);
  6415.  
  6416. #ifndef __GO32__
  6417.       /* If the error that we received was SIGINT, then try again,
  6418.      this is simply an interrupted system call to read ().
  6419.      Otherwise, some error ocurred, also signifying EOF. */
  6420.       if (errno != EINTR)
  6421.     return (EOF);
  6422. #endif /* !__GO32__ */
  6423.     }
  6424. }
  6425.  
  6426. #if defined (STATIC_MALLOC)
  6427.  
  6428. /* **************************************************************** */
  6429. /*                                    */
  6430. /*            xmalloc and xrealloc ()                     */
  6431. /*                                    */
  6432. /* **************************************************************** */
  6433.  
  6434. static void memory_error_and_abort ();
  6435.  
  6436. static char *
  6437. xmalloc (bytes)
  6438.      int bytes;
  6439. {
  6440.   char *temp = (char *)malloc (bytes);
  6441.  
  6442.   if (!temp)
  6443.     memory_error_and_abort ();
  6444.   return (temp);
  6445. }
  6446.  
  6447. static char *
  6448. xrealloc (pointer, bytes)
  6449.      char *pointer;
  6450.      int bytes;
  6451. {
  6452.   char *temp;
  6453.  
  6454.   if (!pointer)
  6455.     temp = (char *)malloc (bytes);
  6456.   else
  6457.     temp = (char *)realloc (pointer, bytes);
  6458.  
  6459.   if (!temp)
  6460.     memory_error_and_abort ();
  6461.  
  6462.   return (temp);
  6463. }
  6464.  
  6465. static void
  6466. memory_error_and_abort ()
  6467. {
  6468.   fprintf (stderr, "readline: Out of virtual memory!\n");
  6469.   abort ();
  6470. }
  6471. #endif /* STATIC_MALLOC */
  6472.  
  6473.  
  6474. /* **************************************************************** */
  6475. /*                                    */
  6476. /*            Testing Readline                */
  6477. /*                                    */
  6478. /* **************************************************************** */
  6479.  
  6480. #if defined (TEST)
  6481.  
  6482. main ()
  6483. {
  6484.   HIST_ENTRY **history_list ();
  6485.   char *temp = (char *)NULL;
  6486.   char *prompt = "readline% ";
  6487.   int done = 0;
  6488.  
  6489.   while (!done)
  6490.     {
  6491.       temp = readline (prompt);
  6492.  
  6493.       /* Test for EOF. */
  6494.       if (!temp)
  6495.     exit (1);
  6496.  
  6497.       /* If there is anything on the line, print it and remember it. */
  6498.       if (*temp)
  6499.     {
  6500.       fprintf (stderr, "%s\r\n", temp);
  6501.       add_history (temp);
  6502.     }
  6503.  
  6504.       /* Check for `command' that we handle. */
  6505.       if (strcmp (temp, "quit") == 0)
  6506.     done = 1;
  6507.  
  6508.       if (strcmp (temp, "list") == 0)
  6509.     {
  6510.       HIST_ENTRY **list = history_list ();
  6511.       register int i;
  6512.       if (list)
  6513.         {
  6514.           for (i = 0; list[i]; i++)
  6515.         {
  6516.           fprintf (stderr, "%d: %s\r\n", i, list[i]->line);
  6517.           free (list[i]->line);
  6518.         }
  6519.           free (list);
  6520.         }
  6521.     }
  6522.       free (temp);
  6523.     }
  6524. }
  6525.  
  6526. #endif /* TEST */
  6527.  
  6528.  
  6529. /*
  6530.  * Local variables:
  6531.  * compile-command: "gcc -g -traditional -I. -I.. -DTEST -o readline readline.c keymaps.o funmap.o history.o -ltermcap"
  6532.  * end:
  6533.  */
  6534.